clean up ast_java: NewExpression, NewArrayExpression, Cast, VariableDeclaration
ast_java doesn't need to know about Type in JavaTypeNamespace.
This commit removes Type in NewExpression, NewArrayExpression, Cast, VariableDeclaration
And make it not include "type_java.h" anymore.
Bug: 110967839
Test: ./runtests.sh
Test: m -j
Change-Id: I1700d936780ab9c7ca4c2411d4687010a28590ad
diff --git a/ast_java.cpp b/ast_java.cpp
index 1dd3818..ab297ef 100644
--- a/ast_java.cpp
+++ b/ast_java.cpp
@@ -15,9 +15,7 @@
*/
#include "ast_java.h"
-
#include "code_writer.h"
-#include "type_java.h"
using std::vector;
using std::string;
@@ -237,9 +235,9 @@
to->Write(")");
}
-NewExpression::NewExpression(const Type* t) : type(t) {}
+NewExpression::NewExpression(const std::string& n) : instantiableName(n) {}
-NewExpression::NewExpression(const Type* t, int argc = 0, ...) : type(t) {
+NewExpression::NewExpression(const std::string& n, int argc = 0, ...) : instantiableName(n) {
va_list args;
va_start(args, argc);
init(argc, args);
@@ -254,31 +252,28 @@
}
void NewExpression::Write(CodeWriter* to) const {
- to->Write("new %s(", this->type->InstantiableName().c_str());
+ to->Write("new %s(", this->instantiableName.c_str());
WriteArgumentList(to, this->arguments);
to->Write(")");
}
-NewArrayExpression::NewArrayExpression(const Type* t, Expression* s)
- : type(t), size(s) {}
+NewArrayExpression::NewArrayExpression(const std::string& t, Expression* s) : type(t), size(s) {}
void NewArrayExpression::Write(CodeWriter* to) const {
- to->Write("new %s[", this->type->JavaType().c_str());
+ to->Write("new %s[", this->type.c_str());
size->Write(to);
to->Write("]");
}
-Cast::Cast(const Type* t, Expression* e) : type(t), expression(e) {}
+Cast::Cast(const std::string& t, Expression* e) : type(t), expression(e) {}
void Cast::Write(CodeWriter* to) const {
- to->Write("((%s)", this->type->JavaType().c_str());
+ to->Write("((%s)", this->type.c_str());
expression->Write(to);
to->Write(")");
}
-VariableDeclaration::VariableDeclaration(Variable* l, Expression* r,
- const Type* c)
- : lvalue(l), cast(c), rvalue(r) {}
+VariableDeclaration::VariableDeclaration(Variable* l, Expression* r) : lvalue(l), rvalue(r) {}
VariableDeclaration::VariableDeclaration(Variable* l) : lvalue(l) {}
@@ -286,9 +281,6 @@
this->lvalue->WriteDeclaration(to);
if (this->rvalue != nullptr) {
to->Write(" = ");
- if (this->cast != nullptr) {
- to->Write("(%s)", this->cast->JavaType().c_str());
- }
this->rvalue->Write(to);
}
to->Write(";\n");
diff --git a/ast_java.h b/ast_java.h
index 33bc3be..e9560ea 100644
--- a/ast_java.h
+++ b/ast_java.h
@@ -50,8 +50,6 @@
namespace aidl {
namespace java {
-class Type;
-
// Write the modifiers that are set in both mod and mask
void WriteModifiers(CodeWriter* to, int mod, int mask);
@@ -201,11 +199,11 @@
};
struct NewExpression : public Expression {
- const Type* type;
+ const std::string instantiableName;
std::vector<Expression*> arguments;
- explicit NewExpression(const Type* type);
- NewExpression(const Type* type, int argc, ...);
+ explicit NewExpression(const std::string& name);
+ NewExpression(const std::string& name, int argc, ...);
virtual ~NewExpression() = default;
void Write(CodeWriter* to) const override;
@@ -214,32 +212,30 @@
};
struct NewArrayExpression : public Expression {
- const Type* type;
+ const std::string type;
Expression* size;
- NewArrayExpression(const Type* type, Expression* size);
+ NewArrayExpression(const std::string& type, Expression* size);
virtual ~NewArrayExpression() = default;
void Write(CodeWriter* to) const override;
};
struct Cast : public Expression {
- const Type* type = nullptr;
+ const std::string type;
Expression* expression = nullptr;
Cast() = default;
- Cast(const Type* type, Expression* expression);
+ Cast(const std::string& type, Expression* expression);
virtual ~Cast() = default;
void Write(CodeWriter* to) const override;
};
struct VariableDeclaration : public Statement {
Variable* lvalue = nullptr;
- const Type* cast = nullptr;
Expression* rvalue = nullptr;
explicit VariableDeclaration(Variable* lvalue);
- VariableDeclaration(Variable* lvalue, Expression* rvalue,
- const Type* cast = nullptr);
+ VariableDeclaration(Variable* lvalue, Expression* rvalue);
virtual ~VariableDeclaration() = default;
void Write(CodeWriter* to) const override;
};
diff --git a/code_writer.h b/code_writer.h
index 8baf6ec..8a70a25 100644
--- a/code_writer.h
+++ b/code_writer.h
@@ -17,6 +17,7 @@
#pragma once
#include <memory>
+#include <ostream>
#include <string>
#include <stdio.h>
diff --git a/generate_java_binder.cpp b/generate_java_binder.cpp
index 847e65c..7a0700a 100644
--- a/generate_java_binder.cpp
+++ b/generate_java_binder.cpp
@@ -276,8 +276,7 @@
queryLocalInterface->arguments.push_back(new LiteralExpression("DESCRIPTOR"));
IInterfaceType* iinType = new IInterfaceType(types);
Variable* iin = new Variable(iinType->JavaType(), "iin");
- VariableDeclaration* iinVd =
- new VariableDeclaration(iin, queryLocalInterface, nullptr);
+ VariableDeclaration* iinVd = new VariableDeclaration(iin, queryLocalInterface);
m->statements->Add(iinVd);
// Ensure the instance type of the local object is as expected.
@@ -293,11 +292,10 @@
IfStatement* instOfStatement = new IfStatement();
instOfStatement->expression = new Comparison(iinNotNull, "&&", instOfCheck);
instOfStatement->statements = new StatementBlock;
- instOfStatement->statements->Add(
- new ReturnStatement(new Cast(interfaceType, iin)));
+ instOfStatement->statements->Add(new ReturnStatement(new Cast(interfaceType->JavaType(), iin)));
m->statements->Add(instOfStatement);
- NewExpression* ne = new NewExpression(interfaceType->GetProxy());
+ NewExpression* ne = new NewExpression(interfaceType->GetProxy()->InstantiableName());
ne->arguments.push_back(obj);
m->statements->Add(new ReturnStatement(ne));
@@ -363,8 +361,7 @@
lencheck->expression = new Comparison(len, "<", new LiteralExpression("0"));
lencheck->statements->Add(new Assignment(v, NULL_VALUE));
lencheck->elseif = new IfStatement();
- lencheck->elseif->statements->Add(
- new Assignment(v, new NewArrayExpression(t, len)));
+ lencheck->elseif->statements->Add(new Assignment(v, new NewArrayExpression(t->JavaType(), len)));
addTo->Add(lencheck);
}
@@ -459,7 +456,7 @@
statements->Add(new LiteralStatement(code));
} else {
if (!arg->GetType().IsArray()) {
- statements->Add(new Assignment(v, new NewExpression(t)));
+ statements->Add(new Assignment(v, new NewExpression(t->InstantiableName())));
} else {
generate_new_array(t, statements, v, transact_data, types);
}