ast_java: shared_ptr everywhere

So that these things will be deleted so that we can turn on leak
detection and so that checks in the destructor (like asserting that
"CheckValid" is called) are actually meaningful.

(note, this is more straightforward than simply rewriting this to not
use ast_java at all due to things like outline methods)

Bug: 37749857
Test: runtests.sh
Test: less leaks reported
Change-Id: Icc59069b9e3b660d0878bc338c86c30b80bb5e18
diff --git a/ast_java.cpp b/ast_java.cpp
index 5eab218..f997494 100644
--- a/ast_java.cpp
+++ b/ast_java.cpp
@@ -65,7 +65,7 @@
   }
 }
 
-void WriteArgumentList(CodeWriter* to, const vector<Expression*>& arguments) {
+void WriteArgumentList(CodeWriter* to, const vector<std::shared_ptr<Expression>>& arguments) {
   size_t N = arguments.size();
   for (size_t i = 0; i < N; i++) {
     arguments[i]->Write(to);
@@ -75,7 +75,7 @@
   }
 }
 
-Field::Field(int m, Variable* v) : ClassElement(), modifiers(m), variable(v) {}
+Field::Field(int m, std::shared_ptr<Variable> v) : ClassElement(), modifiers(m), variable(v) {}
 
 void Field::Write(CodeWriter* to) const {
   if (this->comment.length() != 0) {
@@ -113,13 +113,14 @@
 
 void Variable::Write(CodeWriter* to) const { to->Write("%s", name.c_str()); }
 
-FieldVariable::FieldVariable(Expression* o, const string& n) : receiver(o), name(n) {}
+FieldVariable::FieldVariable(std::shared_ptr<Expression> o, const string& n)
+    : receiver(o), name(n) {}
 
 FieldVariable::FieldVariable(const string& c, const string& n) : receiver(c), name(n) {}
 
 void FieldVariable::Write(CodeWriter* to) const {
   visit(
-      overloaded{[&](Expression* e) { e->Write(to); },
+      overloaded{[&](std::shared_ptr<Expression> e) { e->Write(to); },
                  [&](const std::string& s) { to->Write("%s", s.c_str()); }, [](std::monostate) {}},
       this->receiver);
   to->Write(".%s", name.c_str());
@@ -142,24 +143,26 @@
   to->Write("}\n");
 }
 
-void StatementBlock::Add(Statement* statement) {
+void StatementBlock::Add(std::shared_ptr<Statement> statement) {
   this->statements.push_back(statement);
 }
 
-void StatementBlock::Add(Expression* expression) {
-  this->statements.push_back(new ExpressionStatement(expression));
+void StatementBlock::Add(std::shared_ptr<Expression> expression) {
+  this->statements.push_back(std::make_shared<ExpressionStatement>(expression));
 }
 
-ExpressionStatement::ExpressionStatement(Expression* e) : expression(e) {}
+ExpressionStatement::ExpressionStatement(std::shared_ptr<Expression> e) : expression(e) {}
 
 void ExpressionStatement::Write(CodeWriter* to) const {
   this->expression->Write(to);
   to->Write(";\n");
 }
 
-Assignment::Assignment(Variable* l, Expression* r) : lvalue(l), rvalue(r) {}
+Assignment::Assignment(std::shared_ptr<Variable> l, std::shared_ptr<Expression> r)
+    : lvalue(l), rvalue(r) {}
 
-Assignment::Assignment(Variable* l, Expression* r, string c) : lvalue(l), rvalue(r), cast(c) {}
+Assignment::Assignment(std::shared_ptr<Variable> l, std::shared_ptr<Expression> r, string c)
+    : lvalue(l), rvalue(r), cast(c) {}
 
 void Assignment::Write(CodeWriter* to) const {
   this->lvalue->Write(to);
@@ -172,42 +175,24 @@
 
 MethodCall::MethodCall(const string& n) : name(n) {}
 
-MethodCall::MethodCall(const string& n, int argc = 0, ...) : name(n) {
-  va_list args;
-  va_start(args, argc);
-  init(argc, args);
-  va_end(args);
-}
+MethodCall::MethodCall(const string& n, const std::vector<std::shared_ptr<Expression>>& args)
+    : name(n), arguments(args) {}
 
-MethodCall::MethodCall(Expression* o, const string& n) : receiver(o), name(n) {}
+MethodCall::MethodCall(std::shared_ptr<Expression> o, const string& n) : receiver(o), name(n) {}
 
 MethodCall::MethodCall(const std::string& t, const string& n) : receiver(t), name(n) {}
 
-MethodCall::MethodCall(Expression* o, const string& n, int argc = 0, ...) : receiver(o), name(n) {
-  va_list args;
-  va_start(args, argc);
-  init(argc, args);
-  va_end(args);
-}
+MethodCall::MethodCall(std::shared_ptr<Expression> o, const string& n,
+                       const std::vector<std::shared_ptr<Expression>>& args)
+    : receiver(o), name(n), arguments(args) {}
 
-MethodCall::MethodCall(const std::string& t, const string& n, int argc = 0, ...)
-    : receiver(t), name(n) {
-  va_list args;
-  va_start(args, argc);
-  init(argc, args);
-  va_end(args);
-}
-
-void MethodCall::init(int n, va_list args) {
-  for (int i = 0; i < n; i++) {
-    Expression* expression = (Expression*)va_arg(args, void*);
-    this->arguments.push_back(expression);
-  }
-}
+MethodCall::MethodCall(const std::string& t, const string& n,
+                       const std::vector<std::shared_ptr<Expression>>& args)
+    : receiver(t), name(n), arguments(args) {}
 
 void MethodCall::Write(CodeWriter* to) const {
   visit(
-      overloaded{[&](Expression* e) {
+      overloaded{[&](std::shared_ptr<Expression> e) {
                    e->Write(to);
                    to->Write(".");
                  },
@@ -218,7 +203,8 @@
   to->Write(")");
 }
 
-Comparison::Comparison(Expression* l, const string& o, Expression* r)
+Comparison::Comparison(std::shared_ptr<Expression> l, const string& o,
+                       std::shared_ptr<Expression> r)
     : lvalue(l), op(o), rvalue(r) {}
 
 void Comparison::Write(CodeWriter* to) const {
@@ -231,19 +217,9 @@
 
 NewExpression::NewExpression(const std::string& n) : instantiableName(n) {}
 
-NewExpression::NewExpression(const std::string& n, int argc = 0, ...) : instantiableName(n) {
-  va_list args;
-  va_start(args, argc);
-  init(argc, args);
-  va_end(args);
-}
-
-void NewExpression::init(int n, va_list args) {
-  for (int i = 0; i < n; i++) {
-    Expression* expression = (Expression*)va_arg(args, void*);
-    this->arguments.push_back(expression);
-  }
-}
+NewExpression::NewExpression(const std::string& n,
+                             const std::vector<std::shared_ptr<Expression>>& args)
+    : instantiableName(n), arguments(args) {}
 
 void NewExpression::Write(CodeWriter* to) const {
   to->Write("new %s(", this->instantiableName.c_str());
@@ -251,7 +227,8 @@
   to->Write(")");
 }
 
-NewArrayExpression::NewArrayExpression(const std::string& t, Expression* s) : type(t), size(s) {}
+NewArrayExpression::NewArrayExpression(const std::string& t, std::shared_ptr<Expression> s)
+    : type(t), size(s) {}
 
 void NewArrayExpression::Write(CodeWriter* to) const {
   to->Write("new %s[", this->type.c_str());
@@ -259,7 +236,7 @@
   to->Write("]");
 }
 
-Cast::Cast(const std::string& t, Expression* e) : type(t), expression(e) {}
+Cast::Cast(const std::string& t, std::shared_ptr<Expression> e) : type(t), expression(e) {}
 
 void Cast::Write(CodeWriter* to) const {
   to->Write("((%s)", this->type.c_str());
@@ -267,9 +244,10 @@
   to->Write(")");
 }
 
-VariableDeclaration::VariableDeclaration(Variable* l, Expression* r) : lvalue(l), rvalue(r) {}
+VariableDeclaration::VariableDeclaration(std::shared_ptr<Variable> l, std::shared_ptr<Expression> r)
+    : lvalue(l), rvalue(r) {}
 
-VariableDeclaration::VariableDeclaration(Variable* l) : lvalue(l) {}
+VariableDeclaration::VariableDeclaration(std::shared_ptr<Variable> l) : lvalue(l) {}
 
 void VariableDeclaration::Write(CodeWriter* to) const {
   this->lvalue->WriteDeclaration(to);
@@ -293,7 +271,7 @@
   }
 }
 
-ReturnStatement::ReturnStatement(Expression* e) : expression(e) {}
+ReturnStatement::ReturnStatement(std::shared_ptr<Expression> e) : expression(e) {}
 
 void ReturnStatement::Write(CodeWriter* to) const {
   to->Write("return ");
@@ -330,7 +308,7 @@
   statements->Write(to);
 }
 
-SwitchStatement::SwitchStatement(Expression* e) : expression(e) {}
+SwitchStatement::SwitchStatement(std::shared_ptr<Expression> e) : expression(e) {}
 
 void SwitchStatement::Write(CodeWriter* to) const {
   to->Write("switch (");
@@ -477,11 +455,11 @@
   }
 }
 
-Expression* NULL_VALUE = new LiteralExpression("null");
-Expression* THIS_VALUE = new LiteralExpression("this");
-Expression* SUPER_VALUE = new LiteralExpression("super");
-Expression* TRUE_VALUE = new LiteralExpression("true");
-Expression* FALSE_VALUE = new LiteralExpression("false");
+std::shared_ptr<Expression> NULL_VALUE = std::make_shared<LiteralExpression>("null");
+std::shared_ptr<Expression> THIS_VALUE = std::make_shared<LiteralExpression>("this");
+std::shared_ptr<Expression> SUPER_VALUE = std::make_shared<LiteralExpression>("super");
+std::shared_ptr<Expression> TRUE_VALUE = std::make_shared<LiteralExpression>("true");
+std::shared_ptr<Expression> FALSE_VALUE = std::make_shared<LiteralExpression>("false");
 }  // namespace java
 }  // namespace aidl
 }  // namespace android