Stop storing the direction buffer
We pull the information out of the direction buffer_type immediately and pack
it safely in to privates where it can be accessed with sane accessors.
Test: unit tests
Bug: 24410295
Change-Id: I84e4cf50aad9202210fc9c3db5dd78858991ecd7
Signed-off-by: Casey Dahlin <sadmac@google.com>
diff --git a/aidl_language.cpp b/aidl_language.cpp
index c7ed1e6..3395a3e 100644
--- a/aidl_language.cpp
+++ b/aidl_language.cpp
@@ -44,9 +44,47 @@
}
AidlArgument::AidlArgument(buffer_type direction, type_type type, buffer_type name)
- : direction(direction),
- name(name),
- type(type) {}
+ : name(name),
+ type(type) {
+ direction_specified_ = direction.data != nullptr;
+
+ if (! direction_specified_) {
+ direction_ = AidlArgument::IN_DIR;
+ } else if (! strcmp(direction.data, "in")) {
+ direction_ = AidlArgument::IN_DIR;
+ } else if (! strcmp(direction.data, "out")) {
+ direction_ = AidlArgument::OUT_DIR;
+ } else {
+ direction_ = AidlArgument::INOUT_DIR;
+ }
+
+}
+
+string AidlArgument::ToString() const {
+ string ret;
+
+ if (direction_specified_) {
+ switch(direction_) {
+ case AidlArgument::IN_DIR:
+ ret += "in ";
+ break;
+ case AidlArgument::OUT_DIR:
+ ret += "out ";
+ break;
+ case AidlArgument::INOUT_DIR:
+ ret += "inout ";
+ break;
+ }
+ }
+
+ ret += string(type.type.data);
+ if (type.array_token.data)
+ ret += string(type.array_token.data);
+ ret += " ";
+ ret += string(name.data);
+
+ return ret;
+}
string Parser::FileName() {
return filename_;
diff --git a/aidl_language.h b/aidl_language.h
index be6e4a2..362f9ea 100644
--- a/aidl_language.h
+++ b/aidl_language.h
@@ -56,14 +56,22 @@
class AidlArgument : public AidlNode {
public:
+ enum Direction { IN_DIR = 1, OUT_DIR = 2, INOUT_DIR = 3 };
+
AidlArgument(buffer_type direction, type_type type, buffer_type name);
virtual ~AidlArgument() = default;
- buffer_type direction;
+ Direction GetDirection() const { return direction_; }
+ bool DirectionWasSpecified() const { return direction_specified_; }
+ std::string ToString() const;
+
buffer_type name;
type_type type;
private:
+ Direction direction_;
+ bool direction_specified_;
+
DISALLOW_COPY_AND_ASSIGN(AidlArgument);
};
@@ -123,13 +131,6 @@
extern "C" {
#endif
-// in, out or inout
-enum {
- IN_PARAMETER = 1,
- OUT_PARAMETER = 2,
- INOUT_PARAMETER = 3
-};
-
// callbacks from within the parser
// these functions all take ownership of the strings
struct ParserCallbacks {
diff --git a/generate_cpp.cpp b/generate_cpp.cpp
index 93a6282..059c31e 100644
--- a/generate_cpp.cpp
+++ b/generate_cpp.cpp
@@ -72,7 +72,7 @@
for (const unique_ptr<AidlArgument>& arg : *method->args) {
args.push_back(GetCPPVarDec(
types, &arg->type, arg->name.Literal(),
- OUT_PARAMETER & convert_direction(arg->direction.data)));
+ AidlArgument::OUT_DIR & arg->GetDirection()));
}
string return_arg = GetCPPVarDec(types, &method->type, "_aidl_return", true);
diff --git a/generate_java_binder.cpp b/generate_java_binder.cpp
index b801319..89df307 100644
--- a/generate_java_binder.cpp
+++ b/generate_java_binder.cpp
@@ -322,7 +322,7 @@
c->statements->Add(new VariableDeclaration(v));
- if (convert_direction(arg->direction.data) & IN_PARAMETER) {
+ if (arg->GetDirection() & AidlArgument::IN_DIR) {
generate_create_from_parcel(t, c->statements, v,
stubClass->transact_data, &cl);
} else {
@@ -377,7 +377,7 @@
const Type* t = types->Find(arg->type.type.data);
Variable* v = stubArgs.Get(i++);
- if (convert_direction(arg->direction.data) & OUT_PARAMETER) {
+ if (arg->GetDirection() & AidlArgument::OUT_DIR) {
generate_write_to_parcel(t, c->statements, v,
stubClass->transact_reply,
Type::PARCELABLE_WRITE_RETURN_VALUE);
@@ -440,8 +440,8 @@
for (const std::unique_ptr<AidlArgument>& arg : *method->args) {
const Type* t = types->Find(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) {
+ AidlArgument::Direction dir = arg->GetDirection();
+ if (dir == AidlArgument::OUT_DIR && arg->type.dimension != 0) {
IfStatement* checklen = new IfStatement();
checklen->expression = new Comparison(v, "==", NULL_VALUE);
checklen->statements->Add(new MethodCall(_data, "writeInt", 1,
@@ -451,7 +451,7 @@
1, new FieldVariable(v, "length")));
tryStatement->statements->Add(checklen);
}
- else if (dir & IN_PARAMETER) {
+ else if (dir & AidlArgument::IN_DIR) {
generate_write_to_parcel(t, tryStatement->statements, v, _data, 0);
}
}
@@ -481,7 +481,7 @@
for (const std::unique_ptr<AidlArgument>& arg : *method->args) {
const Type* t = types->Find(arg->type.type.data);
Variable* v = new Variable(t, arg->name.data, arg->type.dimension);
- if (convert_direction(arg->direction.data) & OUT_PARAMETER) {
+ 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 6700c81..cca2021 100644
--- a/parse_helpers.cpp
+++ b/parse_helpers.cpp
@@ -54,19 +54,6 @@
return rv;
}
-int convert_direction(const char* direction) {
- if (direction == NULL) {
- return IN_PARAMETER;
- }
- if (0 == strcmp(direction, "in")) {
- return IN_PARAMETER;
- }
- if (0 == strcmp(direction, "out")) {
- return OUT_PARAMETER;
- }
- return INOUT_PARAMETER;
-}
-
bool is_java_keyword(const char* str) {
static const std::vector<std::string> kJavaKeywords{
"abstract", "assert", "boolean", "break", "byte",
diff --git a/parse_helpers.h b/parse_helpers.h
index 6b50e52..2e77624 100644
--- a/parse_helpers.h
+++ b/parse_helpers.h
@@ -25,8 +25,6 @@
// we rely on the input matching the import regex from below
char* parse_import_statement(const char* text);
-int convert_direction(const char* direction);
-
bool is_java_keyword(const char* str);
char* cpp_strdup(const char* in);
diff --git a/type_namespace.cpp b/type_namespace.cpp
index 4e1951b..07837c6 100644
--- a/type_namespace.cpp
+++ b/type_namespace.cpp
@@ -87,7 +87,7 @@
return false;
}
- if (a.direction.data == nullptr &&
+ if (!a.DirectionWasSpecified() &&
(a.type.dimension != 0 || t->CanBeOutParameter())) {
cerr << error_prefix << StringPrintf(
"'%s %s' can be an out parameter, so you must declare it as in,"
@@ -95,20 +95,19 @@
return false;
}
- if (convert_direction(a.direction.data) != IN_PARAMETER &&
+ if (a.GetDirection() != AidlArgument::IN_DIR &&
!t->CanBeOutParameter() &&
a.type.dimension == 0) {
cerr << error_prefix << StringPrintf(
- "'%s %s %s' can only be an in parameter.",
- a.direction.data, a.type.type.data, a.name.data) << endl;
+ "'%s' can only be an in parameter.",
+ a.ToString().c_str()) << endl;
return false;
}
if (a.type.dimension > 0 && !t->CanBeArray()) {
cerr << error_prefix << StringPrintf(
- "'%s %s%s %s' cannot be an array.",
- a.direction.data, a.type.type.data, a.type.array_token.data,
- a.name.data) << endl;
+ "'%s' cannot be an array.",
+ a.ToString().c_str()) << endl;
return false;
}