Don't allow non-const pointers to Type instances

This makes the namespace into a read only datastructure of types
with a few methods to load types during parsing.

Bug: 24303749
Test: compiles, unittests

Change-Id: I0873e520d771b348a1a8ec9515b6e7b9a8a29298
diff --git a/aidl.cpp b/aidl.cpp
index 3c57e6a..01a1399 100644
--- a/aidl.cpp
+++ b/aidl.cpp
@@ -195,7 +195,7 @@
             return 1;
         }
 
-        Type* old = NAMES.Find(type->QualifiedName());
+        const Type* old = NAMES.Find(type->QualifiedName());
         if (old == NULL) {
             NAMES.Add(type);
 
@@ -247,7 +247,7 @@
     int err = 0;
 
     // return type
-    Type* returnType = NAMES.Search(m->type.type.data);
+    const Type* returnType = NAMES.Search(m->type.type.data);
     if (returnType == NULL) {
         fprintf(stderr, "%s:%d unknown return type %s\n", filename,
                     m->type.type.lineno, m->type.type.data);
@@ -280,7 +280,7 @@
 
     arg_type* arg = m->args;
     while (arg) {
-        Type* t = NAMES.Search(arg->type.type.data);
+        const Type* t = NAMES.Search(arg->type.type.data);
 
         // check the arg type
         if (t == NULL) {
diff --git a/ast_java.cpp b/ast_java.cpp
index 8a4b64e..b3c3a3f 100644
--- a/ast_java.cpp
+++ b/ast_java.cpp
@@ -93,7 +93,7 @@
 }
 
 void
-Field::GatherTypes(set<Type*>* types) const
+Field::GatherTypes(set<const Type*>* types) const
 {
     types->insert(this->variable->type);
 }
@@ -154,14 +154,14 @@
 {
 }
 
-Variable::Variable(Type* t, const string& n)
+Variable::Variable(const Type* t, const string& n)
     :type(t),
      name(n),
      dimension(0)
 {
 }
 
-Variable::Variable(Type* t, const string& n, int d)
+Variable::Variable(const Type* t, const string& n, int d)
     :type(t),
      name(n),
      dimension(d)
@@ -173,7 +173,7 @@
 }
 
 void
-Variable::GatherTypes(set<Type*>* types) const
+Variable::GatherTypes(set<const Type*>* types) const
 {
     types->insert(this->type);
 }
@@ -202,7 +202,7 @@
 {
 }
 
-FieldVariable::FieldVariable(Type* c, const string& n)
+FieldVariable::FieldVariable(const Type* c, const string& n)
     :object(NULL),
      clazz(c),
      name(n)
@@ -331,7 +331,7 @@
 {
 }
 
-MethodCall::MethodCall(Type* t, const string& n)
+MethodCall::MethodCall(const Type* t, const string& n)
     :obj(NULL),
      clazz(t),
      name(n)
@@ -349,7 +349,7 @@
   va_end(args);
 }
 
-MethodCall::MethodCall(Type* t, const string& n, int argc = 0, ...)
+MethodCall::MethodCall(const Type* t, const string& n, int argc = 0, ...)
     :obj(NULL),
      clazz(t),
      name(n)
@@ -409,12 +409,12 @@
     to->Write(")");
 }
 
-NewExpression::NewExpression(Type* t)
+NewExpression::NewExpression(const Type* t)
     :type(t)
 {
 }
 
-NewExpression::NewExpression(Type* t, int argc = 0, ...)
+NewExpression::NewExpression(const Type* t, int argc = 0, ...)
     :type(t)
 {
   va_list args;
@@ -444,7 +444,7 @@
     to->Write(")");
 }
 
-NewArrayExpression::NewArrayExpression(Type* t, Expression* s)
+NewArrayExpression::NewArrayExpression(const Type* t, Expression* s)
     :type(t),
      size(s)
 {
@@ -498,7 +498,7 @@
 {
 }
 
-Cast::Cast(Type* t, Expression* e)
+Cast::Cast(const Type* t, Expression* e)
     :type(t),
      expression(e)
 {
@@ -516,7 +516,7 @@
     to->Write(")");
 }
 
-VariableDeclaration::VariableDeclaration(Variable* l, Expression* r, Type* c)
+VariableDeclaration::VariableDeclaration(Variable* l, Expression* r, const Type* c)
     :lvalue(l),
      cast(c),
      rvalue(r)
@@ -729,7 +729,7 @@
 }
 
 void
-Method::GatherTypes(set<Type*>* types) const
+Method::GatherTypes(set<const Type*>* types) const
 {
     size_t N, i;
 
@@ -811,7 +811,7 @@
 }
 
 void
-Class::GatherTypes(set<Type*>* types) const
+Class::GatherTypes(set<const Type*>* types) const
 {
     int N, i;
 
diff --git a/ast_java.h b/ast_java.h
index b9c898e..401018b 100644
--- a/ast_java.h
+++ b/ast_java.h
@@ -57,7 +57,7 @@
     ClassElement();
     virtual ~ClassElement();
 
-    virtual void GatherTypes(set<Type*>* types) const = 0;
+    virtual void GatherTypes(set<const Type*>* types) const = 0;
     virtual void Write(CodeWriter* to) const = 0;
 };
 
@@ -88,16 +88,16 @@
 
 struct Variable : public Expression
 {
-    Type* type;
+    const Type* type;
     string name;
     int dimension;
 
     Variable();
-    Variable(Type* type, const string& name);
-    Variable(Type* type, const string& name, int dimension);
+    Variable(const Type* type, const string& name);
+    Variable(const Type* type, const string& name, int dimension);
     virtual ~Variable();
 
-    virtual void GatherTypes(set<Type*>* types) const;
+    virtual void GatherTypes(set<const Type*>* types) const;
     void WriteDeclaration(CodeWriter* to) const;
     void Write(CodeWriter* to) const;
 };
@@ -105,11 +105,11 @@
 struct FieldVariable : public Expression
 {
     Expression* object;
-    Type* clazz;
+    const Type* clazz;
     string name;
 
     FieldVariable(Expression* object, const string& name);
-    FieldVariable(Type* clazz, const string& name);
+    FieldVariable(const Type* clazz, const string& name);
     virtual ~FieldVariable();
 
     void Write(CodeWriter* to) const;
@@ -126,7 +126,7 @@
     Field(int modifiers, Variable* variable);
     virtual ~Field();
 
-    virtual void GatherTypes(set<Type*>* types) const;
+    virtual void GatherTypes(set<const Type*>* types) const;
     virtual void Write(CodeWriter* to) const;
 };
 
@@ -172,7 +172,7 @@
 struct MethodCall : public Expression
 {
     Expression* obj;
-    Type* clazz;
+    const Type* clazz;
     string name;
     vector<Expression*> arguments;
     vector<string> exceptions;
@@ -180,9 +180,9 @@
     MethodCall(const string& name);
     MethodCall(const string& name, int argc, ...);
     MethodCall(Expression* obj, const string& name);
-    MethodCall(Type* clazz, const string& name);
+    MethodCall(const Type* clazz, const string& name);
     MethodCall(Expression* obj, const string& name, int argc, ...);
-    MethodCall(Type* clazz, const string& name, int argc, ...);
+    MethodCall(const Type* clazz, const string& name, int argc, ...);
     virtual ~MethodCall();
     virtual void Write(CodeWriter* to) const;
 
@@ -203,11 +203,11 @@
 
 struct NewExpression : public Expression
 {
-    Type* type;
+    const Type* type;
     vector<Expression*> arguments;
 
-    NewExpression(Type* type);
-    NewExpression(Type* type, int argc, ...);
+    NewExpression(const Type* type);
+    NewExpression(const Type* type, int argc, ...);
     virtual ~NewExpression();
     virtual void Write(CodeWriter* to) const;
 
@@ -217,10 +217,10 @@
 
 struct NewArrayExpression : public Expression
 {
-    Type* type;
+    const Type* type;
     Expression* size;
 
-    NewArrayExpression(Type* type, Expression* size);
+    NewArrayExpression(const Type* type, Expression* size);
     virtual ~NewArrayExpression();
     virtual void Write(CodeWriter* to) const;
 };
@@ -239,11 +239,11 @@
 
 struct Cast : public Expression
 {
-    Type* type;
+    const Type* type;
     Expression* expression;
 
     Cast();
-    Cast(Type* type, Expression* expression);
+    Cast(const Type* type, Expression* expression);
     virtual ~Cast();
     virtual void Write(CodeWriter* to) const;
 };
@@ -251,11 +251,11 @@
 struct VariableDeclaration : public Statement
 {
     Variable* lvalue;
-    Type* cast;
+    const Type* cast;
     Expression* rvalue;
 
     VariableDeclaration(Variable* lvalue);
-    VariableDeclaration(Variable* lvalue, Expression* rvalue, Type* cast = NULL);
+    VariableDeclaration(Variable* lvalue, Expression* rvalue, const Type* cast = NULL);
     virtual ~VariableDeclaration();
     virtual void Write(CodeWriter* to) const;
 };
@@ -340,17 +340,17 @@
 {
     string comment;
     int modifiers;
-    Type* returnType;
+    const Type* returnType;
     size_t returnTypeDimension;
     string name;
     vector<Variable*> parameters;
-    vector<Type*> exceptions;
+    vector<const Type*> exceptions;
     StatementBlock* statements;
 
     Method();
     virtual ~Method();
 
-    virtual void GatherTypes(set<Type*>* types) const;
+    virtual void GatherTypes(set<const Type*>* types) const;
     virtual void Write(CodeWriter* to) const;
 };
 
@@ -364,15 +364,15 @@
     string comment;
     int modifiers;
     int what;               // CLASS or INTERFACE
-    Type* type;
-    Type* extends;
-    vector<Type*> interfaces;
+    const Type* type;
+    const Type* extends;
+    vector<const Type*> interfaces;
     vector<ClassElement*> elements;
 
     Class();
     virtual ~Class();
 
-    virtual void GatherTypes(set<Type*>* types) const;
+    virtual void GatherTypes(set<const Type*>* types) const;
     virtual void Write(CodeWriter* to) const;
 };
 
@@ -381,7 +381,7 @@
     string comment;
     string package;
     string originalSrc;
-    set<Type*> imports;
+    set<const Type*> imports;
     vector<Class*> classes;
 
     Document();
diff --git a/generate_java.cpp b/generate_java.cpp
index 98f93bc..09a4410 100644
--- a/generate_java.cpp
+++ b/generate_java.cpp
@@ -18,7 +18,7 @@
 }
 
 Variable*
-VariableFactory::Get(Type* type)
+VariableFactory::Get(const Type* type)
 {
     char name[100];
     sprintf(name, "%s%d", m_base.c_str(), m_index);
diff --git a/generate_java.h b/generate_java.h
index 5fd92ed..12d9f57 100644
--- a/generate_java.h
+++ b/generate_java.h
@@ -28,7 +28,7 @@
     using Type = android::aidl::Type;
 
     VariableFactory(const string& base); // base must be short
-    Variable* Get(Type* type);
+    Variable* Get(const Type* type);
     Variable* Get(int index);
 private:
     vector<Variable*> m_vars;
diff --git a/generate_java_binder.cpp b/generate_java_binder.cpp
index 60c0c12..f44254f 100644
--- a/generate_java_binder.cpp
+++ b/generate_java_binder.cpp
@@ -15,7 +15,7 @@
 class StubClass : public Class
 {
 public:
-    StubClass(Type* type, Type* interfaceType);
+    StubClass(const Type* type, const Type* interfaceType);
     virtual ~StubClass();
 
     Variable* transact_code;
@@ -24,10 +24,10 @@
     Variable* transact_flags;
     SwitchStatement* transact_switch;
 private:
-    void make_as_interface(Type* interfaceType);
+    void make_as_interface(const Type* interfaceType);
 };
 
-StubClass::StubClass(Type* type, Type* interfaceType)
+StubClass::StubClass(const Type* type, const Type* interfaceType)
     :Class()
 {
     this->comment = "/** Local-side IPC implementation stub class. */";
@@ -97,7 +97,7 @@
 }
 
 void
-StubClass::make_as_interface(Type *interfaceType)
+StubClass::make_as_interface(const Type *interfaceType)
 {
     Variable* obj = new Variable(IBINDER_TYPE, "obj");
 
@@ -155,14 +155,14 @@
 class ProxyClass : public Class
 {
 public:
-    ProxyClass(Type* type, InterfaceType* interfaceType);
+    ProxyClass(const Type* type, const InterfaceType* interfaceType);
     virtual ~ProxyClass();
 
     Variable* mRemote;
     bool mOneWay;
 };
 
-ProxyClass::ProxyClass(Type* type, InterfaceType* interfaceType)
+ProxyClass::ProxyClass(const Type* type, const InterfaceType* interfaceType)
     :Class()
 {
     this->modifiers = PRIVATE | STATIC;
@@ -201,7 +201,7 @@
 
 // =================================================
 static void
-generate_new_array(Type* t, StatementBlock* addTo, Variable* v,
+generate_new_array(const Type* t, StatementBlock* addTo, Variable* v,
                             Variable* parcel)
 {
     Variable* len = new Variable(INT_TYPE, v->name + "_length");
@@ -216,7 +216,7 @@
 }
 
 static void
-generate_write_to_parcel(Type* t, StatementBlock* addTo, Variable* v,
+generate_write_to_parcel(const Type* t, StatementBlock* addTo, Variable* v,
                             Variable* parcel, int flags)
 {
     if (v->dimension == 0) {
@@ -228,7 +228,7 @@
 }
 
 static void
-generate_create_from_parcel(Type* t, StatementBlock* addTo, Variable* v,
+generate_create_from_parcel(const Type* t, StatementBlock* addTo, Variable* v,
                             Variable* parcel, Variable** cl)
 {
     if (v->dimension == 0) {
@@ -240,7 +240,7 @@
 }
 
 static void
-generate_read_from_parcel(Type* t, StatementBlock* addTo, Variable* v,
+generate_read_from_parcel(const Type* t, StatementBlock* addTo, Variable* v,
                             Variable* parcel, Variable** cl)
 {
     if (v->dimension == 0) {
@@ -309,7 +309,7 @@
     VariableFactory stubArgs("_arg");
     arg = method->args;
     while (arg != NULL) {
-        Type* t = NAMES.Search(arg->type.type.data);
+        const Type* t = NAMES.Search(arg->type.type.data);
         Variable* v = stubArgs.Get(t);
         v->dimension = arg->type.dimension;
 
@@ -370,7 +370,7 @@
     i = 0;
     arg = method->args;
     while (arg != NULL) {
-        Type* t = NAMES.Search(arg->type.type.data);
+        const Type* t = NAMES.Search(arg->type.type.data);
         Variable* v = stubArgs.Get(i++);
 
         if (convert_direction(arg->direction.data) & OUT_PARAMETER) {
@@ -437,7 +437,7 @@
     // the parameters
     arg = method->args;
     while (arg != NULL) {
-        Type* t = NAMES.Search(arg->type.type.data);
+        const Type* t = NAMES.Search(arg->type.type.data);
         Variable* v = new Variable(t, arg->name.data, arg->type.dimension);
         int dir = convert_direction(arg->direction.data);
         if (dir == OUT_PARAMETER && arg->type.dimension != 0) {
@@ -480,7 +480,7 @@
         // the out/inout parameters
         arg = method->args;
         while (arg != NULL) {
-            Type* t = NAMES.Search(arg->type.type.data);
+            const Type* t = NAMES.Search(arg->type.type.data);
             Variable* v = new Variable(t, arg->name.data, arg->type.dimension);
             if (convert_direction(arg->direction.data) & OUT_PARAMETER) {
                 generate_read_from_parcel(t, tryStatement->statements,
@@ -522,7 +522,7 @@
 Class*
 generate_binder_interface_class(const interface_type* iface)
 {
-    InterfaceType* interfaceType = static_cast<InterfaceType*>(
+    const InterfaceType* interfaceType = static_cast<const InterfaceType*>(
         NAMES.Find(iface->package, iface->name.data));
 
     // the interface class
diff --git a/type_java.cpp b/type_java.cpp
index 1dc3ab8..58e8c5e 100644
--- a/type_java.cpp
+++ b/type_java.cpp
@@ -147,7 +147,7 @@
 }
 
 static Type* make_generic_type(const string& package, const string& name,
-                               const vector<Type*>& args) {
+                               const vector<const Type*>& args) {
   if (package == "java.util" && name == "List") {
     return new GenericListType("java.util", "List", args);
   }
@@ -780,7 +780,7 @@
 // ================================================================
 
 GenericType::GenericType(const string& package, const string& name,
-                         const vector<Type*>& args)
+                         const vector<const Type*>& args)
     : Type(package, name, BUILT_IN, true, true) {
   m_args = args;
 
@@ -789,7 +789,7 @@
   string gen = "<";
   int N = args.size();
   for (int i = 0; i < N; i++) {
-    Type* t = args[i];
+    const Type* t = args[i];
     gen += t->QualifiedName();
     if (i != N - 1) {
       gen += ',';
@@ -800,7 +800,7 @@
   SetQualifiedName(m_importName + gen);
 }
 
-const vector<Type*>& GenericType::GenericArgumentTypes() const {
+const vector<const Type*>& GenericType::GenericArgumentTypes() const {
   return m_args;
 }
 
@@ -826,7 +826,7 @@
 // ================================================================
 
 GenericListType::GenericListType(const string& package, const string& name,
-                                 const vector<Type*>& args)
+                                 const vector<const Type*>& args)
     : GenericType(package, name, args), m_creator(args[0]->CreatorName()) {}
 
 string GenericListType::CreatorName() const {
@@ -894,8 +894,8 @@
   }
 }
 
-void Namespace::Add(Type* type) {
-  Type* t = Find(type->QualifiedName());
+void Namespace::Add(const Type* type) {
+  const Type* t = Find(type->QualifiedName());
   if (t == NULL) {
     m_types.push_back(type);
   }
@@ -911,7 +911,7 @@
   m_generics.push_back(g);
 }
 
-Type* Namespace::Find(const string& name) const {
+const Type* Namespace::Find(const string& name) const {
   int N = m_types.size();
   for (int i = 0; i < N; i++) {
     if (m_types[i]->QualifiedName() == name) {
@@ -921,7 +921,7 @@
   return NULL;
 }
 
-Type* Namespace::Find(const char* package, const char* name) const {
+const Type* Namespace::Find(const char* package, const char* name) const {
   string s;
   if (package != nullptr && *package != '\0') {
     s += package;
@@ -943,9 +943,9 @@
   return r;
 }
 
-Type* Namespace::Search(const string& name) {
+const Type* Namespace::Search(const string& name) {
   // an exact match wins
-  Type* result = Find(name);
+  const Type* result = Find(name);
   if (result != NULL) {
     return result;
   }
@@ -982,7 +982,7 @@
   // generics within generics like Java does, because we're really limiting
   // them to just built-in container classes, at least for now.  Our syntax
   // ensures this right now as well.
-  vector<Type*> args;
+  vector<const Type*> args;
   size_t start = baseIndex + 1;
   size_t end = start;
   while (normalized[start] != '\0') {
@@ -991,7 +991,7 @@
       end = normalized.find('>', start);
     }
     string s(normalized.c_str() + start, end - start);
-    Type* t = this->Search(s);
+    const Type* t = this->Search(s);
     if (t == NULL) {
       // maybe we should print a warning here?
       return NULL;
@@ -1036,7 +1036,7 @@
 void Namespace::Dump() const {
   int n = m_types.size();
   for (int i = 0; i < n; i++) {
-    Type* t = m_types[i];
+    const Type* t = m_types[i];
     printf("type: package=%s name=%s qualifiedName=%s\n", t->Package().c_str(),
            t->Name().c_str(), t->QualifiedName().c_str());
   }
diff --git a/type_java.h b/type_java.h
index 8528c8e..4006158 100644
--- a/type_java.h
+++ b/type_java.h
@@ -346,9 +346,9 @@
 class GenericType : public Type {
  public:
   GenericType(const string& package, const string& name,
-              const vector<Type*>& args);
+              const vector<const Type*>& args);
 
-  const vector<Type*>& GenericArgumentTypes() const;
+  const vector<const Type*>& GenericArgumentTypes() const;
   string GenericArguments() const;
 
   string ImportType() const override;
@@ -363,7 +363,7 @@
  private:
   string m_genericArguments;
   string m_importName;
-  vector<Type*> m_args;
+  vector<const Type*> m_args;
 };
 
 class ClassLoaderType : public Type {
@@ -374,7 +374,7 @@
 class GenericListType : public GenericType {
  public:
   GenericListType(const string& package, const string& name,
-                  const vector<Type*>& args);
+                  const vector<const Type*>& args);
 
   string CreatorName() const override;
   string InstantiableName() const override;
@@ -394,17 +394,17 @@
  public:
   Namespace();
   ~Namespace();
-  void Add(Type* type);
+  void Add(const Type* type);
 
   // args is the number of template types (what is this called?)
   void AddGenericType(const string& package, const string& name, int args);
 
   // lookup a specific class name
-  Type* Find(const string& name) const;
-  Type* Find(const char* package, const char* name) const;
+  const Type* Find(const string& name) const;
+  const Type* Find(const char* package, const char* name) const;
 
   // try to search by either a full name or a partial name
-  Type* Search(const string& name);
+  const Type* Search(const string& name);
 
   void Dump() const;
 
@@ -418,7 +418,7 @@
 
   const Generic* search_generic(const string& name) const;
 
-  vector<Type*> m_types;
+  vector<const Type*> m_types;
   vector<Generic> m_generics;
 };