Remove TypeNamespace
1. Remove TypeNamesapce
2. In aidl.cpp, just validate language specific restriction(validate_type)
instead of register_type
Test: m
Test: ./runtests.sh
Bug: 110967839
Change-Id: I7406f5b326af4e2342059aef9277ddfb24775ce9
diff --git a/Android.bp b/Android.bp
index 44581fd..44ef1b2 100644
--- a/Android.bp
+++ b/Android.bp
@@ -62,9 +62,6 @@
"line_reader.cpp",
"io_delegate.cpp",
"options.cpp",
- "type_cpp.cpp",
- "type_java.cpp",
- "type_namespace.cpp",
],
yacc: {
gen_location_hh: true,
@@ -124,8 +121,6 @@
"tests/test_data_ping_responder.cpp",
"tests/test_data_string_constants.cpp",
"tests/test_util.cpp",
- "type_cpp_unittest.cpp",
- "type_java_unittest.cpp",
],
static_libs: [
diff --git a/aidl.cpp b/aidl.cpp
index 28f791e..3483593 100644
--- a/aidl.cpp
+++ b/aidl.cpp
@@ -46,9 +46,6 @@
#include "logging.h"
#include "options.h"
#include "os.h"
-#include "type_cpp.h"
-#include "type_java.h"
-#include "type_namespace.h"
#ifndef O_BINARY
# define O_BINARY 0
@@ -152,62 +149,6 @@
return valid;
}
-bool register_types(const AidlStructuredParcelable* parcel, TypeNamespace* types) {
- for (const auto& v : parcel->GetFields()) {
- if (!types->MaybeAddContainerType(v->GetType())) {
- return false;
- }
-
- const ValidatableType* type = types->GetReturnType(v->GetType(), *parcel);
- if (type == nullptr) {
- return false;
- }
- v->GetMutableType()->SetLanguageType(type);
- }
- return true;
-}
-
-bool register_types(const AidlInterface* c, TypeNamespace* types) {
- for (const auto& m : c->GetMethods()) {
- if (!types->MaybeAddContainerType(m->GetType())) {
- return false;
- }
-
- const ValidatableType* return_type = types->GetReturnType(m->GetType(), *c);
-
- if (return_type == nullptr) {
- return false;
- }
- m->GetMutableType()->SetLanguageType(return_type);
-
- set<string> argument_names;
-
- int index = 1;
- for (const auto& arg : m->GetArguments()) {
- if (!types->MaybeAddContainerType(arg->GetType())) {
- return false;
- }
-
- const ValidatableType* arg_type = types->GetArgType(*arg, index, *c);
- if (arg_type == nullptr) {
- return false;
- }
- arg->GetMutableType()->SetLanguageType(arg_type);
- }
- }
-
- for (const std::unique_ptr<AidlConstantDeclaration>& constant : c->GetConstantDeclarations()) {
- AidlTypeSpecifier* specifier = constant->GetMutableType();
- const ValidatableType* return_type = types->GetReturnType(*specifier, *c);
- if (return_type == nullptr) {
- return false;
- }
- specifier->SetLanguageType(return_type);
- }
-
- return true;
-}
-
bool write_dep_file(const Options& options, const AidlDefinedType& defined_type,
const vector<string>& imports, const IoDelegate& io_delegate,
const string& input_file, const string& output_file) {
@@ -399,7 +340,7 @@
namespace internals {
bool parse_preprocessed_file(const IoDelegate& io_delegate, const string& filename,
- TypeNamespace* types, AidlTypenames& typenames) {
+ AidlTypenames* typenames) {
bool success = true;
unique_ptr<LineReader> line_reader = io_delegate.GetLineReader(filename);
if (!line_reader) {
@@ -436,20 +377,17 @@
}
AidlParcelable* doc = new AidlParcelable(
location, new AidlQualifiedName(location, class_name, ""), package, "" /* comments */);
- types->AddParcelableType(*doc, filename);
- typenames.AddPreprocessedType(unique_ptr<AidlParcelable>(doc));
+ typenames->AddPreprocessedType(unique_ptr<AidlParcelable>(doc));
} else if (decl == "structured_parcelable") {
auto temp = new std::vector<std::unique_ptr<AidlVariableDeclaration>>();
AidlStructuredParcelable* doc =
new AidlStructuredParcelable(location, new AidlQualifiedName(location, class_name, ""),
package, "" /* comments */, temp);
- types->AddParcelableType(*doc, filename);
- typenames.AddPreprocessedType(unique_ptr<AidlStructuredParcelable>(doc));
+ typenames->AddPreprocessedType(unique_ptr<AidlStructuredParcelable>(doc));
} else if (decl == "interface") {
auto temp = new std::vector<std::unique_ptr<AidlMember>>();
AidlInterface* doc = new AidlInterface(location, class_name, "", false, temp, package);
- types->AddBinderType(*doc, filename);
- typenames.AddPreprocessedType(unique_ptr<AidlInterface>(doc));
+ typenames->AddPreprocessedType(unique_ptr<AidlInterface>(doc));
} else {
success = false;
break;
@@ -464,7 +402,7 @@
}
AidlError load_and_validate_aidl(const std::string& input_file_name, const Options& options,
- const IoDelegate& io_delegate, TypeNamespace* types,
+ const IoDelegate& io_delegate, AidlTypenames* typenames,
vector<AidlDefinedType*>* defined_types,
vector<string>* imported_files) {
AidlError err = AidlError::OK;
@@ -474,8 +412,7 @@
//////////////////////////////////////////////////////////////////////////
// Parse the main input file
- std::unique_ptr<Parser> main_parser =
- Parser::Parse(input_file_name, io_delegate, types->typenames_);
+ std::unique_ptr<Parser> main_parser = Parser::Parse(input_file_name, io_delegate, *typenames);
if (main_parser == nullptr) {
return AidlError::PARSE_ERROR;
}
@@ -483,13 +420,10 @@
AIDL_ERROR(input_file_name) << "You must declare only one type per a file.";
return AidlError::BAD_TYPE;
}
- if (!types->AddDefinedTypes(main_parser->GetDefinedTypes(), input_file_name)) {
- return AidlError::BAD_TYPE;
- }
// Import the preprocessed file
for (const string& s : options.PreprocessedFiles()) {
- if (!parse_preprocessed_file(io_delegate, s, types, types->typenames_)) {
+ if (!parse_preprocessed_file(io_delegate, s, typenames)) {
err = AidlError::BAD_PRE_PROCESSED_FILE;
}
}
@@ -523,7 +457,7 @@
set<string> import_candidates(type_from_import_statements);
import_candidates.insert(unresolved_types.begin(), unresolved_types.end());
for (const auto& import : import_candidates) {
- if (types->HasImportType(import)) {
+ if (typenames->IsIgnorableImport(import)) {
// There are places in the Android tree where an import doesn't resolve,
// but we'll pick the type up through the preprocessed types.
// This seems like an error, but legacy support demands we support it...
@@ -541,16 +475,12 @@
import_paths.emplace_back(import_path);
- std::unique_ptr<Parser> import_parser =
- Parser::Parse(import_path, io_delegate, types->typenames_);
+ std::unique_ptr<Parser> import_parser = Parser::Parse(import_path, io_delegate, *typenames);
if (import_parser == nullptr) {
cerr << "error while importing " << import_path << " for " << import << endl;
err = AidlError::BAD_IMPORT;
continue;
}
- if (!types->AddDefinedTypes(import_parser->GetDefinedTypes(), import_path)) {
- return AidlError::BAD_TYPE;
- }
}
if (err != AidlError::OK) {
return err;
@@ -559,16 +489,12 @@
for (const auto& imported_file : options.ImportFiles()) {
import_paths.emplace_back(imported_file);
- std::unique_ptr<Parser> import_parser =
- Parser::Parse(imported_file, io_delegate, types->typenames_);
+ std::unique_ptr<Parser> import_parser = Parser::Parse(imported_file, io_delegate, *typenames);
if (import_parser == nullptr) {
AIDL_ERROR(imported_file) << "error while importing " << imported_file;
err = AidlError::BAD_IMPORT;
continue;
}
- if (!types->AddDefinedTypes(import_parser->GetDefinedTypes(), imported_file)) {
- return AidlError::BAD_TYPE;
- }
}
if (err != AidlError::OK) {
return err;
@@ -581,34 +507,10 @@
// using fully qualified names.
return AidlError::BAD_TYPE;
}
- if (!is_check_api) {
- for (const auto defined_type : main_parser->GetDefinedTypes()) {
- AidlInterface* interface = defined_type->AsInterface();
- AidlStructuredParcelable* parcelable = defined_type->AsStructuredParcelable();
-
- // Link the AIDL type with the type of the target language. This will
- // be removed when the migration to AidlTypenames is done.
- defined_type->SetLanguageType(types->GetDefinedType(*defined_type));
-
- if (interface != nullptr) {
- if (!register_types(interface, types)) {
- return AidlError::BAD_TYPE;
- }
- }
- if (parcelable != nullptr) {
- if (!register_types(parcelable, types)) {
- return AidlError::BAD_TYPE;
- }
- }
- }
- }
-
//////////////////////////////////////////////////////////////////////////
// Validation phase
//////////////////////////////////////////////////////////////////////////
- AidlTypenames& typenames = types->typenames_;
-
// For legacy reasons, by default, compiling an unstructured parcelable (which contains no output)
// is allowed. This must not be returned as an error until the very end of this procedure since
// this may be considered a success, and we should first check that there are not other, more
@@ -618,9 +520,20 @@
const int num_defined_types = main_parser->GetDefinedTypes().size();
for (const auto defined_type : main_parser->GetDefinedTypes()) {
CHECK(defined_type != nullptr);
+
+ // Language specific validation
+ AidlError check_result = AidlError::OK;
+ if (!defined_type->LanguageSpecificCheckValid(options.TargetLanguage())) {
+ return AidlError::BAD_TYPE;
+ }
+
+ if (check_result != AidlError::OK) {
+ return check_result;
+ }
+
AidlParcelable* unstructuredParcelable = defined_type->AsUnstructuredParcelable();
if (unstructuredParcelable != nullptr) {
- if (!unstructuredParcelable->CheckValid(typenames)) {
+ if (!unstructuredParcelable->CheckValid(*typenames)) {
return AidlError::BAD_TYPE;
}
bool isStable = unstructuredParcelable->IsStableParcelable();
@@ -662,7 +575,7 @@
if (!is_check_api) {
// No need to do this for check api because all typespecs are already
// using fully qualified name and we don't import in AIDL files.
- if (!defined_type->CheckValid(typenames)) {
+ if (!defined_type->CheckValid(*typenames)) {
return AidlError::BAD_TYPE;
}
}
@@ -672,7 +585,7 @@
if (options.Version() > 0) {
AidlTypeSpecifier* ret =
new AidlTypeSpecifier(AIDL_LOCATION_HERE, "int", false, nullptr, "");
- ret->Resolve(typenames);
+ ret->Resolve(*typenames);
vector<unique_ptr<AidlArgument>>* args = new vector<unique_ptr<AidlArgument>>();
AidlMethod* method =
new AidlMethod(AIDL_LOCATION_HERE, false, ret, "getInterfaceVersion", args, "",
@@ -685,7 +598,7 @@
}
}
- typenames.IterateTypes([&](const AidlDefinedType& type) {
+ typenames->IterateTypes([&](const AidlDefinedType& type) {
if (options.IsStructured() && type.AsUnstructuredParcelable() != nullptr &&
!type.AsUnstructuredParcelable()->IsStableParcelable()) {
err = AidlError::NOT_STRUCTURED;
@@ -724,30 +637,13 @@
int compile_aidl(const Options& options, const IoDelegate& io_delegate) {
const Options::Language lang = options.TargetLanguage();
for (const string& input_file : options.InputFiles()) {
- // Create type namespace that will hold the types identified by the parser.
- // This two namespaces that are specific to the target language will be
- // unified to AidlTypenames which is agnostic to the target language.
- cpp::TypeNamespace cpp_types;
- cpp_types.Init();
-
- java::JavaTypeNamespace java_types;
- java_types.Init();
-
- TypeNamespace* types;
- if (options.IsCppOutput()) {
- types = &cpp_types;
- } else if (lang == Options::Language::JAVA) {
- types = &java_types;
- } else {
- LOG(FATAL) << "Unsupported target language." << endl;
- return 1;
- }
+ AidlTypenames typenames;
vector<AidlDefinedType*> defined_types;
vector<string> imported_files;
- AidlError aidl_err = internals::load_and_validate_aidl(input_file, options, io_delegate, types,
- &defined_types, &imported_files);
+ AidlError aidl_err = internals::load_and_validate_aidl(
+ input_file, options, io_delegate, &typenames, &defined_types, &imported_files);
bool allowError = aidl_err == AidlError::FOUND_PARCELABLE && !options.FailOnParcelable();
if (aidl_err != AidlError::OK && !allowError) {
return 1;
@@ -772,15 +668,14 @@
bool success = false;
if (lang == Options::Language::CPP) {
- success = cpp::GenerateCpp(output_file_name, options, cpp_types.typenames_, *defined_type,
- io_delegate);
+ success =
+ cpp::GenerateCpp(output_file_name, options, typenames, *defined_type, io_delegate);
} else if (lang == Options::Language::NDK) {
- ndk::GenerateNdk(output_file_name, options, cpp_types.typenames_, *defined_type,
- io_delegate);
+ ndk::GenerateNdk(output_file_name, options, typenames, *defined_type, io_delegate);
success = true;
} else if (lang == Options::Language::JAVA) {
- success = java::generate_java(output_file_name, defined_type, java_types.typenames_,
- io_delegate, options);
+ success =
+ java::generate_java(output_file_name, defined_type, typenames, io_delegate, options);
} else {
LOG(FATAL) << "Should not reach here" << endl;
return 1;
@@ -796,13 +691,12 @@
bool dump_mappings(const Options& options, const IoDelegate& io_delegate) {
android::aidl::mappings::SignatureMap all_mappings;
for (const string& input_file : options.InputFiles()) {
- java::JavaTypeNamespace java_types;
- java_types.Init();
+ AidlTypenames typenames;
vector<AidlDefinedType*> defined_types;
vector<string> imported_files;
AidlError aidl_err = internals::load_and_validate_aidl(
- input_file, options, io_delegate, &java_types, &defined_types, &imported_files);
+ input_file, options, io_delegate, &typenames, &defined_types, &imported_files);
if (aidl_err != AidlError::OK) {
LOG(WARNING) << "AIDL file is invalid.\n";
continue;
@@ -849,10 +743,9 @@
bool dump_api(const Options& options, const IoDelegate& io_delegate) {
for (const auto& file : options.InputFiles()) {
- java::JavaTypeNamespace ns;
- ns.Init();
+ AidlTypenames typenames;
vector<AidlDefinedType*> defined_types;
- if (internals::load_and_validate_aidl(file, options, io_delegate, &ns, &defined_types,
+ if (internals::load_and_validate_aidl(file, options, io_delegate, &typenames, &defined_types,
nullptr) == AidlError::OK) {
for (const auto type : defined_types) {
unique_ptr<CodeWriter> writer =
diff --git a/aidl.h b/aidl.h
index 5b5799c..955b280 100644
--- a/aidl.h
+++ b/aidl.h
@@ -25,7 +25,6 @@
#include "import_resolver.h"
#include "io_delegate.h"
#include "options.h"
-#include "type_namespace.h"
namespace android {
namespace aidl {
@@ -56,12 +55,12 @@
namespace internals {
AidlError load_and_validate_aidl(const std::string& input_file_name, const Options& options,
- const IoDelegate& io_delegate, TypeNamespace* types,
+ const IoDelegate& io_delegate, AidlTypenames* typenames,
vector<AidlDefinedType*>* defined_types,
vector<string>* imported_files);
bool parse_preprocessed_file(const IoDelegate& io_delegate, const std::string& filename,
- TypeNamespace* types, AidlTypenames& typenames);
+ AidlTypenames* typenames);
} // namespace internals
diff --git a/aidl_apicheck.cpp b/aidl_apicheck.cpp
index 4bedd17..3030ebd 100644
--- a/aidl_apicheck.cpp
+++ b/aidl_apicheck.cpp
@@ -16,8 +16,8 @@
#include "aidl.h"
#include "aidl_language.h"
#include "import_resolver.h"
+#include "logging.h"
#include "options.h"
-#include "type_java.h"
#include <map>
#include <string>
@@ -179,9 +179,7 @@
CHECK(options.IsStructured());
CHECK(options.InputFiles().size() == 2) << "--checkapi requires two inputs "
<< "but got " << options.InputFiles().size();
-
- java::JavaTypeNamespace old_ns;
- old_ns.Init();
+ AidlTypenames old_tns;
const string old_dir = options.InputFiles().at(0);
vector<AidlDefinedType*> old_types;
vector<string> old_files = io_delegate.ListFiles(old_dir);
@@ -191,7 +189,7 @@
}
for (const auto& file : old_files) {
vector<AidlDefinedType*> types;
- if (internals::load_and_validate_aidl(file, options, io_delegate, &old_ns, &types,
+ if (internals::load_and_validate_aidl(file, options, io_delegate, &old_tns, &types,
nullptr /* imported_files */) != AidlError::OK) {
AIDL_ERROR(file) << "Failed to read.";
return false;
@@ -199,8 +197,7 @@
old_types.insert(old_types.end(), types.begin(), types.end());
}
- java::JavaTypeNamespace new_ns;
- new_ns.Init();
+ AidlTypenames new_tns;
const string new_dir = options.InputFiles().at(1);
vector<AidlDefinedType*> new_types;
vector<string> new_files = io_delegate.ListFiles(new_dir);
@@ -210,7 +207,7 @@
}
for (const auto& file : new_files) {
vector<AidlDefinedType*> types;
- if (internals::load_and_validate_aidl(file, options, io_delegate, &new_ns, &types,
+ if (internals::load_and_validate_aidl(file, options, io_delegate, &new_tns, &types,
nullptr /* imported_files */) != AidlError::OK) {
AIDL_ERROR(file) << "Failed to read.";
return false;
diff --git a/aidl_language.cpp b/aidl_language.cpp
index 6068415..5eeb8ad 100644
--- a/aidl_language.cpp
+++ b/aidl_language.cpp
@@ -17,7 +17,6 @@
#include "aidl_language_y.h"
#include "logging.h"
-#include "type_namespace.h"
#ifdef _WIN32
int isatty(int fd)
@@ -37,6 +36,22 @@
using std::unique_ptr;
using std::vector;
+namespace {
+bool is_java_keyword(const char* str) {
+ static const std::vector<std::string> kJavaKeywords{
+ "abstract", "assert", "boolean", "break", "byte", "case", "catch",
+ "char", "class", "const", "continue", "default", "do", "double",
+ "else", "enum", "extends", "final", "finally", "float", "for",
+ "goto", "if", "implements", "import", "instanceof", "int", "interface",
+ "long", "native", "new", "package", "private", "protected", "public",
+ "return", "short", "static", "strictfp", "super", "switch", "synchronized",
+ "this", "throw", "throws", "transient", "try", "void", "volatile",
+ "while", "true", "false", "null",
+ };
+ return std::find(kJavaKeywords.begin(), kJavaKeywords.end(), str) != kJavaKeywords.end();
+}
+} // namespace
+
void yylex_init(void **);
void yylex_destroy(void *);
void yyset_in(FILE *f, void *);
@@ -780,6 +795,88 @@
return true;
}
+// TODO: we should treat every backend all the same in future.
+bool AidlTypeSpecifier::LanguageSpecificCheckValid(Options::Language lang) const {
+ if (lang == Options::Language::CPP) {
+ if (this->GetName() == "List" && !this->IsGeneric()) {
+ AIDL_ERROR(this) << "List without type isn't supported in cpp.";
+ return false;
+ }
+ }
+ if (this->IsGeneric()) {
+ if (this->GetName() == "List") {
+ if (this->GetTypeParameters().size() != 1) {
+ AIDL_ERROR(this) << "List must have only one type parameter.";
+ return false;
+ }
+ if (lang == Options::Language::CPP) {
+ auto& name = this->GetTypeParameters()[0]->GetName();
+ if (!(name == "String" || name == "IBinder")) {
+ AIDL_ERROR(this) << "List in cpp supports only string and IBinder for now.";
+ return false;
+ }
+ } else if (lang == Options::Language::NDK) {
+ AIDL_ERROR(this) << "NDK backend does not support List yet.";
+ return false;
+ }
+
+ } else if (this->GetName() == "Map") {
+ if (lang != Options::Language::JAVA) {
+ AIDL_ERROR(this) << "Currently, only Java backend supports Map.";
+ return false;
+ }
+ }
+ }
+ return true;
+}
+
+// TODO: we should treat every backend all the same in future.
+bool AidlParcelable::LanguageSpecificCheckValid(Options::Language lang) const {
+ if (lang != Options::Language::JAVA) {
+ if (this->IsStableParcelable()) {
+ AIDL_ERROR(this) << "@JavaOnlyStableParcelable supports only Java target.";
+ return false;
+ }
+ const AidlParcelable* unstructuredParcelable = this->AsUnstructuredParcelable();
+ if (unstructuredParcelable != nullptr) {
+ if (unstructuredParcelable->GetCppHeader().empty()) {
+ AIDL_ERROR(unstructuredParcelable)
+ << "Unstructured parcelable must have C++ header defined.";
+ return false;
+ }
+ }
+ }
+ return true;
+}
+
+// TODO: we should treat every backend all the same in future.
+bool AidlStructuredParcelable::LanguageSpecificCheckValid(Options::Language lang) const {
+ if (!AidlParcelable::LanguageSpecificCheckValid(lang)) {
+ return false;
+ }
+ for (const auto& v : this->GetFields()) {
+ if (!v->GetType().LanguageSpecificCheckValid(lang)) {
+ return false;
+ }
+ }
+ return true;
+}
+
+// TODO: we should treat every backend all the same in future.
+bool AidlInterface::LanguageSpecificCheckValid(Options::Language lang) const {
+ for (const auto& m : this->GetMethods()) {
+ if (!m->GetType().LanguageSpecificCheckValid(lang)) {
+ return false;
+ }
+ for (const auto& arg : m->GetArguments()) {
+ if (!arg->GetType().LanguageSpecificCheckValid(lang)) {
+ return false;
+ }
+ }
+ }
+ return true;
+}
+
AidlInterface::AidlInterface(const AidlLocation& location, const std::string& name,
const std::string& comments, bool oneway,
std::vector<std::unique_ptr<AidlMember>>* members,
@@ -852,6 +949,29 @@
AIDL_ERROR(m) << "oneway method '" << m->GetName() << "' cannot have out parameters";
return false;
}
+ const bool can_be_out = typenames.CanBeOutParameter(arg->GetType());
+ if (!arg->DirectionWasSpecified() && can_be_out) {
+ AIDL_ERROR(arg) << "'" << arg->GetType().ToString()
+ << "' can be an out type, so you must declare it as in, out, or inout.";
+ return false;
+ }
+
+ if (arg->GetDirection() != AidlArgument::IN_DIR && !can_be_out) {
+ AIDL_ERROR(arg) << "'" << arg->ToString() << "' can only be an in parameter.";
+ return false;
+ }
+
+ // check that the name doesn't match a keyword
+ if (is_java_keyword(arg->GetName().c_str())) {
+ AIDL_ERROR(arg) << "Argument name is a Java or aidl keyword";
+ return false;
+ }
+
+ // Reserve a namespace for internal use
+ if (android::base::StartsWith(arg->GetName(), "_aidl")) {
+ AIDL_ERROR(arg) << "Argument name cannot begin with '_aidl'";
+ return false;
+ }
}
auto it = method_names.find(m->GetName());
diff --git a/aidl_language.h b/aidl_language.h
index 3895432..ee8a201 100644
--- a/aidl_language.h
+++ b/aidl_language.h
@@ -3,6 +3,7 @@
#include "aidl_typenames.h"
#include "code_writer.h"
#include "io_delegate.h"
+#include "options.h"
#include <memory>
#include <string>
@@ -16,11 +17,11 @@
using android::aidl::AidlTypenames;
using android::aidl::CodeWriter;
+using android::aidl::Options;
using std::shared_ptr;
using std::string;
using std::unique_ptr;
using std::vector;
-
class AidlNode;
namespace android {
@@ -257,6 +258,7 @@
bool Resolve(android::aidl::AidlTypenames& typenames);
bool CheckValid(const AidlTypenames& typenames) const;
+ bool LanguageSpecificCheckValid(Options::Language lang) const;
void SetLanguageType(const android::aidl::ValidatableType* language_type) {
language_type_ = language_type;
@@ -536,7 +538,7 @@
virtual const AidlParcelable* AsParcelable() const { return nullptr; }
virtual const AidlInterface* AsInterface() const { return nullptr; }
virtual bool CheckValid(const AidlTypenames&) const { return CheckValidAnnotations(); }
-
+ virtual bool LanguageSpecificCheckValid(Options::Language lang) const = 0;
AidlStructuredParcelable* AsStructuredParcelable() {
return const_cast<AidlStructuredParcelable*>(
const_cast<const AidlDefinedType*>(this)->AsStructuredParcelable());
@@ -589,7 +591,7 @@
std::string GetCppHeader() const { return cpp_header_; }
bool CheckValid(const AidlTypenames& typenames) const override;
-
+ bool LanguageSpecificCheckValid(Options::Language lang) const override;
const AidlParcelable* AsParcelable() const override { return this; }
std::string GetPreprocessDeclarationName() const override { return "parcelable"; }
@@ -618,6 +620,7 @@
void Write(CodeWriter* writer) const override;
bool CheckValid(const AidlTypenames& typenames) const override;
+ bool LanguageSpecificCheckValid(Options::Language lang) const override;
private:
const std::vector<std::unique_ptr<AidlVariableDeclaration>> variables_;
@@ -645,6 +648,7 @@
void Write(CodeWriter* writer) const override;
bool CheckValid(const AidlTypenames& typenames) const override;
+ bool LanguageSpecificCheckValid(Options::Language lang) const override;
private:
std::vector<std::unique_ptr<AidlMethod>> methods_;
diff --git a/aidl_typenames.cpp b/aidl_typenames.cpp
index 3b79aba..32b03bd 100644
--- a/aidl_typenames.cpp
+++ b/aidl_typenames.cpp
@@ -81,6 +81,13 @@
return true;
}
+bool AidlTypenames::IsIgnorableImport(const string& import) const {
+ static set<string> ignore_import = {"android.os.IInterface", "android.os.IBinder",
+ "android.os.Parcelable", "android.os.Parcel",
+ "android.content.Context", "java.lang.String"};
+ return ResolveTypename(import).second || ignore_import.find(import) != ignore_import.end();
+}
+
bool AidlTypenames::AddDefinedType(unique_ptr<AidlDefinedType> type) {
const string name = type->GetCanonicalName();
if (defined_types_.find(name) != defined_types_.end()) {
diff --git a/aidl_typenames.h b/aidl_typenames.h
index 74db49c..3a64ef2 100644
--- a/aidl_typenames.h
+++ b/aidl_typenames.h
@@ -58,7 +58,7 @@
const AidlDefinedType* TryGetDefinedType(const string& type_name) const;
pair<string, bool> ResolveTypename(const string& type_name) const;
bool CanBeOutParameter(const AidlTypeSpecifier& type) const;
-
+ bool IsIgnorableImport(const string& import) const;
// Iterates over all defined and then preprocessed types
void IterateTypes(const std::function<void(const AidlDefinedType&)>& body) const;
diff --git a/aidl_unittest.cpp b/aidl_unittest.cpp
index 2a5a5d7..3763f75 100644
--- a/aidl_unittest.cpp
+++ b/aidl_unittest.cpp
@@ -26,10 +26,9 @@
#include "aidl_apicheck.h"
#include "aidl_language.h"
#include "aidl_to_cpp.h"
+#include "aidl_to_java.h"
+#include "options.h"
#include "tests/fake_io_delegate.h"
-#include "type_cpp.h"
-#include "type_java.h"
-#include "type_namespace.h"
using android::aidl::internals::parse_preprocessed_file;
using android::aidl::test::FakeIoDelegate;
@@ -144,8 +143,6 @@
class AidlTest : public ::testing::Test {
protected:
void SetUp() override {
- java_types_.Init();
- cpp_types_.Init();
CaptureStderr();
}
@@ -158,15 +155,15 @@
void AddExpectedStderr(string expected) { expected_stderr_.push_back(expected); }
- AidlDefinedType* Parse(const string& path, const string& contents, TypeNamespace* types,
- AidlError* error = nullptr,
+ AidlDefinedType* Parse(const string& path, const string& contents, AidlTypenames& typenames_,
+ Options::Language lang, AidlError* error = nullptr,
const vector<string> additional_arguments = {}) {
io_delegate_.SetFileContents(path, contents);
vector<string> args;
- if (types == &java_types_) {
- args.emplace_back("aidl");
- } else {
+ if (lang == Options::Language::CPP) {
args.emplace_back("aidl-cpp");
+ } else {
+ args.emplace_back("aidl");
}
for (const string& s : additional_arguments) {
args.emplace_back(s);
@@ -183,7 +180,7 @@
vector<string> imported_files;
ImportResolver import_resolver{io_delegate_, path, import_paths_, {}};
AidlError actual_error = ::android::aidl::internals::load_and_validate_aidl(
- path, options, io_delegate_, types, &defined_types, &imported_files);
+ path, options, io_delegate_, &typenames_, &defined_types, &imported_files);
if (error != nullptr) {
*error = actual_error;
@@ -202,18 +199,21 @@
vector<string> preprocessed_files_;
set<string> import_paths_;
vector<string> expected_stderr_;
- java::JavaTypeNamespace java_types_;
- cpp::TypeNamespace cpp_types_;
+ AidlTypenames typenames_;
};
TEST_F(AidlTest, AcceptMissingPackage) {
- EXPECT_NE(nullptr, Parse("IFoo.aidl", "interface IFoo { }", &java_types_));
- EXPECT_NE(nullptr, Parse("IFoo.aidl", "interface IFoo { }", &cpp_types_));
+ EXPECT_NE(nullptr, Parse("IFoo.aidl", "interface IFoo { }", typenames_, Options::Language::JAVA));
+ typenames_.Reset();
+ EXPECT_NE(nullptr, Parse("IFoo.aidl", "interface IFoo { }", typenames_, Options::Language::CPP));
}
TEST_F(AidlTest, EndsInSingleLineComment) {
- EXPECT_NE(nullptr, Parse("IFoo.aidl", "interface IFoo { } // foo", &java_types_));
- EXPECT_NE(nullptr, Parse("IFoo.aidl", "interface IFoo { } // foo", &cpp_types_));
+ EXPECT_NE(nullptr,
+ Parse("IFoo.aidl", "interface IFoo { } // foo", typenames_, Options::Language::JAVA));
+ typenames_.Reset();
+ EXPECT_NE(nullptr,
+ Parse("IFoo.aidl", "interface IFoo { } // foo", typenames_, Options::Language::CPP));
}
TEST_F(AidlTest, RejectsArraysOfBinders) {
@@ -224,18 +224,22 @@
string contents = "package foo;\n"
"import bar.IBar;\n"
"interface IFoo { void f(in IBar[] input); }";
- EXPECT_EQ(nullptr, Parse(path, contents, &java_types_));
- EXPECT_EQ(nullptr, Parse(path, contents, &cpp_types_));
+ EXPECT_EQ(nullptr, Parse(path, contents, typenames_, Options::Language::JAVA));
+ typenames_.Reset();
+ EXPECT_EQ(nullptr, Parse(path, contents, typenames_, Options::Language::CPP));
}
TEST_F(AidlTest, SupportOnlyOutParameters) {
string interface_list = "package a; interface IBar { void f(out List bar); }";
string interface_ibinder = "package a; interface IBaz { void f(out IBinder bar); }";
// List without type isn't supported in cpp.
- EXPECT_EQ(nullptr, Parse("a/IBar.aidl", interface_list, &cpp_types_));
- EXPECT_NE(nullptr, Parse("a/IBar.aidl", interface_list, &java_types_));
- EXPECT_EQ(nullptr, Parse("a/IBaz.aidl", interface_ibinder, &cpp_types_));
- EXPECT_EQ(nullptr, Parse("a/IBaz.aidl", interface_ibinder, &java_types_));
+ EXPECT_EQ(nullptr, Parse("a/IBar.aidl", interface_list, typenames_, Options::Language::CPP));
+ typenames_.Reset();
+ EXPECT_NE(nullptr, Parse("a/IBar.aidl", interface_list, typenames_, Options::Language::JAVA));
+ typenames_.Reset();
+ EXPECT_EQ(nullptr, Parse("a/IBaz.aidl", interface_ibinder, typenames_, Options::Language::CPP));
+ typenames_.Reset();
+ EXPECT_EQ(nullptr, Parse("a/IBaz.aidl", interface_ibinder, typenames_, Options::Language::JAVA));
}
TEST_F(AidlTest, RejectsOnewayOutParameters) {
@@ -243,49 +247,55 @@
"package a; oneway interface IFoo { void f(out int bar); }";
string oneway_method =
"package a; interface IBar { oneway void f(out int bar); }";
- EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", oneway_interface, &cpp_types_));
- EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", oneway_interface, &java_types_));
- EXPECT_EQ(nullptr, Parse("a/IBar.aidl", oneway_method, &cpp_types_));
- EXPECT_EQ(nullptr, Parse("a/IBar.aidl", oneway_method, &java_types_));
+ EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", oneway_interface, typenames_, Options::Language::CPP));
+ typenames_.Reset();
+ EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", oneway_interface, typenames_, Options::Language::JAVA));
+ typenames_.Reset();
+ EXPECT_EQ(nullptr, Parse("a/IBar.aidl", oneway_method, typenames_, Options::Language::CPP));
+ typenames_.Reset();
+ EXPECT_EQ(nullptr, Parse("a/IBar.aidl", oneway_method, typenames_, Options::Language::JAVA));
}
TEST_F(AidlTest, RejectsOnewayNonVoidReturn) {
string oneway_method = "package a; interface IFoo { oneway int f(); }";
- EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", oneway_method, &cpp_types_));
- EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", oneway_method, &java_types_));
+ EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", oneway_method, typenames_, Options::Language::CPP));
+ typenames_.Reset();
+ EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", oneway_method, typenames_, Options::Language::JAVA));
}
TEST_F(AidlTest, RejectsNullablePrimitive) {
string oneway_method = "package a; interface IFoo { @nullable int f(); }";
- EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", oneway_method, &cpp_types_));
- EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", oneway_method, &java_types_));
+ EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", oneway_method, typenames_, Options::Language::CPP));
+ typenames_.Reset();
+ EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", oneway_method, typenames_, Options::Language::JAVA));
}
TEST_F(AidlTest, RejectsDuplicatedArgumentNames) {
string method = "package a; interface IFoo { void f(int a, int a); }";
- EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", method, &cpp_types_));
- EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", method, &java_types_));
+ EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", method, typenames_, Options::Language::CPP));
+ typenames_.Reset();
+ EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", method, typenames_, Options::Language::JAVA));
}
TEST_F(AidlTest, RejectsDuplicatedAnnotationParams) {
string method = "package a; interface IFoo { @UnsupportedAppUsage(foo=1, foo=2)void f(); }";
- EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", method, &cpp_types_));
- EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", method, &java_types_));
+ EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", method, typenames_, Options::Language::CPP));
+ typenames_.Reset();
+ EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", method, typenames_, Options::Language::JAVA));
}
TEST_F(AidlTest, ParsesNullableAnnotation) {
for (auto is_nullable: {true, false}) {
- auto parse_result = Parse(
- "a/IFoo.aidl",
- StringPrintf( "package a; interface IFoo {%s String f(); }",
- (is_nullable) ? "@nullable" : ""),
- &cpp_types_);
+ auto parse_result = Parse("a/IFoo.aidl",
+ StringPrintf("package a; interface IFoo {%s String f(); }",
+ (is_nullable) ? "@nullable" : ""),
+ typenames_, Options::Language::CPP);
ASSERT_NE(nullptr, parse_result);
const AidlInterface* interface = parse_result->AsInterface();
ASSERT_NE(nullptr, interface);
ASSERT_FALSE(interface->GetMethods().empty());
EXPECT_EQ(interface->GetMethods()[0]->GetType().IsNullable(), is_nullable);
- cpp_types_.typenames_.Reset();
+ typenames_.Reset();
}
}
@@ -293,51 +303,52 @@
for (auto is_utf8: {true, false}) {
auto parse_result = Parse(
"a/IFoo.aidl",
- StringPrintf( "package a; interface IFoo {%s String f(); }",
- (is_utf8) ? "@utf8InCpp" : ""),
- &cpp_types_);
+ StringPrintf("package a; interface IFoo {%s String f(); }", (is_utf8) ? "@utf8InCpp" : ""),
+ typenames_, Options::Language::CPP);
ASSERT_NE(nullptr, parse_result);
const AidlInterface* interface = parse_result->AsInterface();
ASSERT_NE(nullptr, interface);
ASSERT_FALSE(interface->GetMethods().empty());
EXPECT_EQ(interface->GetMethods()[0]->GetType().IsUtf8InCpp(), is_utf8);
- cpp_types_.typenames_.Reset();
+ typenames_.Reset();
}
}
TEST_F(AidlTest, VintfRequiresStructuredAndStability) {
AidlError error;
- auto parse_result = Parse("IFoo.aidl", "@VintfStability interface IFoo {}", &cpp_types_, &error);
+ auto parse_result = Parse("IFoo.aidl", "@VintfStability interface IFoo {}", typenames_,
+ Options::Language::CPP, &error);
ASSERT_EQ(AidlError::NOT_STRUCTURED, error);
ASSERT_EQ(nullptr, parse_result);
}
TEST_F(AidlTest, VintfRequiresStructured) {
AidlError error;
- auto parse_result = Parse("IFoo.aidl", "@VintfStability interface IFoo {}", &cpp_types_, &error,
- {"--stability", "vintf"});
+ auto parse_result = Parse("IFoo.aidl", "@VintfStability interface IFoo {}", typenames_,
+ Options::Language::CPP, &error, {"--stability", "vintf"});
ASSERT_EQ(AidlError::NOT_STRUCTURED, error);
ASSERT_EQ(nullptr, parse_result);
}
TEST_F(AidlTest, VintfRequiresSpecifiedStability) {
AidlError error;
- auto parse_result = Parse("IFoo.aidl", "@VintfStability interface IFoo {}", &cpp_types_, &error,
- {"--structured"});
+ auto parse_result = Parse("IFoo.aidl", "@VintfStability interface IFoo {}", typenames_,
+ Options::Language::CPP, &error, {"--structured"});
ASSERT_EQ(AidlError::NOT_STRUCTURED, error);
ASSERT_EQ(nullptr, parse_result);
}
TEST_F(AidlTest, ParsesStabilityAnnotations) {
AidlError error;
- auto parse_result = Parse("IFoo.aidl", "@VintfStability interface IFoo {}", &cpp_types_, &error,
- {"--structured", "--stability", "vintf"});
+ auto parse_result =
+ Parse("IFoo.aidl", "@VintfStability interface IFoo {}", typenames_, Options::Language::CPP,
+ &error, {"--structured", "--stability", "vintf"});
ASSERT_EQ(AidlError::OK, error);
ASSERT_NE(nullptr, parse_result);
const AidlInterface* interface = parse_result->AsInterface();
ASSERT_NE(nullptr, interface);
ASSERT_TRUE(interface->IsVintfStability());
- cpp_types_.typenames_.Reset();
+ typenames_.Reset();
}
TEST_F(AidlTest, ParsesJavaOnlyStableParcelable) {
@@ -348,6 +359,8 @@
"a/Foo.aidl", StringPrintf("package a; @JavaOnlyStableParcelable parcelable Foo;"));
EXPECT_EQ(0, ::android::aidl::compile_aidl(java_options, io_delegate_));
+ AddExpectedStderr(
+ "ERROR: a/Foo.aidl:1.48-52: @JavaOnlyStableParcelable supports only Java target.\n");
EXPECT_NE(0, ::android::aidl::compile_aidl(cpp_options, io_delegate_));
}
@@ -355,16 +368,20 @@
string oneway_method = "package a; interface IFoo { oneway void f(int a); }";
string oneway_interface =
"package a; oneway interface IBar { void f(int a); }";
- EXPECT_NE(nullptr, Parse("a/IFoo.aidl", oneway_method, &cpp_types_));
- EXPECT_NE(nullptr, Parse("a/IFoo.aidl", oneway_method, &java_types_));
- EXPECT_NE(nullptr, Parse("a/IBar.aidl", oneway_interface, &cpp_types_));
- EXPECT_NE(nullptr, Parse("a/IBar.aidl", oneway_interface, &java_types_));
+ EXPECT_NE(nullptr, Parse("a/IFoo.aidl", oneway_method, typenames_, Options::Language::CPP));
+ typenames_.Reset();
+ EXPECT_NE(nullptr, Parse("a/IFoo.aidl", oneway_method, typenames_, Options::Language::JAVA));
+ typenames_.Reset();
+ EXPECT_NE(nullptr, Parse("a/IBar.aidl", oneway_interface, typenames_, Options::Language::CPP));
+ typenames_.Reset();
+ EXPECT_NE(nullptr, Parse("a/IBar.aidl", oneway_interface, typenames_, Options::Language::JAVA));
}
TEST_F(AidlTest, AcceptsAnnotatedOnewayMethod) {
string oneway_method = "package a; interface IFoo { @UnsupportedAppUsage oneway void f(int a); }";
- EXPECT_NE(nullptr, Parse("a/IFoo.aidl", oneway_method, &cpp_types_));
- EXPECT_NE(nullptr, Parse("a/IFoo.aidl", oneway_method, &java_types_));
+ EXPECT_NE(nullptr, Parse("a/IFoo.aidl", oneway_method, typenames_, Options::Language::CPP));
+ typenames_.Reset();
+ EXPECT_NE(nullptr, Parse("a/IFoo.aidl", oneway_method, typenames_, Options::Language::JAVA));
}
TEST_F(AidlTest, WritesComments) {
@@ -374,7 +391,7 @@
" /* j */ @nullable String j();"
" /* k */ @UnsupportedAppUsage oneway void k(int a); }";
- auto parse_result = Parse("a/IFoo.aidl", foo_interface, &java_types_);
+ auto parse_result = Parse("a/IFoo.aidl", foo_interface, typenames_, Options::Language::JAVA);
EXPECT_NE(nullptr, parse_result);
EXPECT_EQ("/* foo */", parse_result->GetComments());
@@ -387,19 +404,20 @@
TEST_F(AidlTest, ParsesPreprocessedFile) {
string simple_content = "parcelable a.Foo;\ninterface b.IBar;";
io_delegate_.SetFileContents("path", simple_content);
- EXPECT_FALSE(java_types_.HasTypeByCanonicalName("a.Foo"));
- EXPECT_TRUE(parse_preprocessed_file(io_delegate_, "path", &java_types_, java_types_.typenames_));
- EXPECT_TRUE(java_types_.HasTypeByCanonicalName("a.Foo"));
- EXPECT_TRUE(java_types_.HasTypeByCanonicalName("b.IBar"));
+ EXPECT_FALSE(typenames_.ResolveTypename("a.Foo").second);
+ EXPECT_TRUE(parse_preprocessed_file(io_delegate_, "path", &typenames_));
+ EXPECT_TRUE(typenames_.ResolveTypename("a.Foo").second);
+ EXPECT_TRUE(typenames_.ResolveTypename("b.IBar").second);
}
TEST_F(AidlTest, ParsesPreprocessedFileWithWhitespace) {
string simple_content = "parcelable a.Foo;\n interface b.IBar ;\t";
io_delegate_.SetFileContents("path", simple_content);
- EXPECT_FALSE(java_types_.HasTypeByCanonicalName("a.Foo"));
- EXPECT_TRUE(parse_preprocessed_file(io_delegate_, "path", &java_types_, java_types_.typenames_));
- EXPECT_TRUE(java_types_.HasTypeByCanonicalName("a.Foo"));
- EXPECT_TRUE(java_types_.HasTypeByCanonicalName("b.IBar"));
+
+ EXPECT_FALSE(typenames_.ResolveTypename("a.Foo").second);
+ EXPECT_TRUE(parse_preprocessed_file(io_delegate_, "path", &typenames_));
+ EXPECT_TRUE(typenames_.ResolveTypename("a.Foo").second);
+ EXPECT_TRUE(typenames_.ResolveTypename("b.IBar").second);
}
TEST_F(AidlTest, PreferImportToPreprocessed) {
@@ -408,18 +426,17 @@
"interface IBar {}");
preprocessed_files_.push_back("preprocessed");
import_paths_.emplace("");
- auto parse_result = Parse(
- "p/IFoo.aidl", "package p; import one.IBar; interface IFoo {}",
- &java_types_);
+ auto parse_result = Parse("p/IFoo.aidl", "package p; import one.IBar; interface IFoo {}",
+ typenames_, Options::Language::JAVA);
EXPECT_NE(nullptr, parse_result);
+
// We expect to know about both kinds of IBar
- EXPECT_TRUE(java_types_.HasTypeByCanonicalName("one.IBar"));
- EXPECT_TRUE(java_types_.HasTypeByCanonicalName("another.IBar"));
+ EXPECT_TRUE(typenames_.ResolveTypename("one.IBar").second);
+ EXPECT_TRUE(typenames_.ResolveTypename("another.IBar").second);
// But if we request just "IBar" we should get our imported one.
AidlTypeSpecifier ambiguous_type(AIDL_LOCATION_HERE, "IBar", false, nullptr, "");
- const java::Type* type = java_types_.Find(ambiguous_type);
- ASSERT_TRUE(type);
- EXPECT_EQ("one.IBar", type->CanonicalName());
+ ambiguous_type.Resolve(typenames_);
+ EXPECT_EQ("one.IBar", ambiguous_type.GetName());
}
TEST_F(AidlTest, WritePreprocessedFile) {
@@ -471,10 +488,9 @@
io_delegate_.SetFileContents("p/Outer.aidl",
"package p; parcelable Outer.Inner;");
import_paths_.emplace("");
- auto parse_result = Parse(
- "p/IFoo.aidl",
- "package p; import p.Outer; interface IFoo { void f(in Inner c); }",
- &java_types_);
+ auto parse_result =
+ Parse("p/IFoo.aidl", "package p; import p.Outer; interface IFoo { void f(in Inner c); }",
+ typenames_, Options::Language::JAVA);
EXPECT_EQ(nullptr, parse_result);
}
@@ -482,10 +498,8 @@
io_delegate_.SetFileContents("preprocessed",
"parcelable p.Outer.Inner;");
preprocessed_files_.push_back("preprocessed");
- auto parse_result = Parse(
- "p/IFoo.aidl",
- "package p; interface IFoo { void f(in Inner c); }",
- &java_types_);
+ auto parse_result = Parse("p/IFoo.aidl", "package p; interface IFoo { void f(in Inner c); }",
+ typenames_, Options::Language::JAVA);
// TODO(wiley): This should actually return nullptr because we require
// the outer class name. However, for legacy reasons,
// this behavior must be maintained. b/17415692
@@ -519,23 +533,21 @@
auto parse_result =
Parse("p/IFoo.aidl",
"package p; import o.WhoKnowsWhat; interface IFoo { void f(in WhoKnowsWhat thisIs); }",
- &java_types_, &reported_error, {"--structured"});
+ typenames_, Options::Language::JAVA, &reported_error, {"--structured"});
EXPECT_EQ(nullptr, parse_result);
EXPECT_EQ(AidlError::NOT_STRUCTURED, reported_error);
}
TEST_F(AidlTest, FailOnDuplicateConstantNames) {
AidlError reported_error;
- EXPECT_EQ(nullptr,
- Parse("p/IFoo.aidl",
- R"(package p;
+ EXPECT_EQ(nullptr, Parse("p/IFoo.aidl",
+ R"(package p;
interface IFoo {
const String DUPLICATED = "d";
const int DUPLICATED = 1;
}
)",
- &cpp_types_,
- &reported_error));
+ typenames_, Options::Language::CPP, &reported_error));
EXPECT_EQ(AidlError::BAD_TYPE, reported_error);
}
@@ -549,7 +561,7 @@
parcelable StructuredParcelable {}
interface IBaz {}
)",
- &cpp_types_, &reported_error));
+ typenames_, Options::Language::CPP, &reported_error));
// Parse success is important for clear error handling even if the cases aren't
// actually supported in code generation.
EXPECT_EQ(AidlError::BAD_TYPE, reported_error);
@@ -557,35 +569,32 @@
TEST_F(AidlTest, FailOnNoDefinedTypes) {
AidlError reported_error;
- EXPECT_EQ(nullptr, Parse("p/IFoo.aidl", R"(package p;)", &cpp_types_, &reported_error));
+ EXPECT_EQ(nullptr, Parse("p/IFoo.aidl", R"(package p;)", typenames_, Options::Language::CPP,
+ &reported_error));
EXPECT_EQ(AidlError::PARSE_ERROR, reported_error);
}
TEST_F(AidlTest, FailOnMalformedConstHexValue) {
AidlError reported_error;
- EXPECT_EQ(nullptr,
- Parse("p/IFoo.aidl",
- R"(package p;
+ EXPECT_EQ(nullptr, Parse("p/IFoo.aidl",
+ R"(package p;
interface IFoo {
const int BAD_HEX_VALUE = 0xffffffffffffffffff;
}
)",
- &cpp_types_,
- &reported_error));
+ typenames_, Options::Language::CPP, &reported_error));
EXPECT_EQ(AidlError::BAD_TYPE, reported_error);
}
TEST_F(AidlTest, ParsePositiveConstHexValue) {
AidlError reported_error;
- auto cpp_parse_result =
- Parse("p/IFoo.aidl",
- R"(package p;
+ auto cpp_parse_result = Parse("p/IFoo.aidl",
+ R"(package p;
interface IFoo {
const int POSITIVE_HEX_VALUE = 0xf5;
}
)",
- &cpp_types_,
- &reported_error);
+ typenames_, Options::Language::CPP, &reported_error);
EXPECT_NE(nullptr, cpp_parse_result);
const AidlInterface* interface = cpp_parse_result->AsInterface();
ASSERT_NE(nullptr, interface);
@@ -597,15 +606,13 @@
TEST_F(AidlTest, ParseNegativeConstHexValue) {
AidlError reported_error;
- auto cpp_parse_result =
- Parse("p/IFoo.aidl",
- R"(package p;
+ auto cpp_parse_result = Parse("p/IFoo.aidl",
+ R"(package p;
interface IFoo {
const int NEGATIVE_HEX_VALUE = 0xffffffff;
}
)",
- &cpp_types_,
- &reported_error);
+ typenames_, Options::Language::CPP, &reported_error);
EXPECT_NE(nullptr, cpp_parse_result);
const AidlInterface* interface = cpp_parse_result->AsInterface();
ASSERT_NE(nullptr, interface);
@@ -624,12 +631,14 @@
const string input = "package p; import p.Outer; interface IFoo"
" { Outer.Inner get(); }";
- auto cpp_parse_result = Parse(input_path, input, &cpp_types_);
+ auto cpp_parse_result = Parse(input_path, input, typenames_, Options::Language::CPP);
EXPECT_NE(nullptr, cpp_parse_result);
- auto cpp_type = cpp_types_.FindTypeByCanonicalName("p.Outer.Inner");
- ASSERT_NE(nullptr, cpp_type);
+
+ auto pair = typenames_.ResolveTypename("p.Outer.Inner");
+ EXPECT_TRUE(pair.second);
// C++ uses "::" instead of "." to refer to a inner class.
- EXPECT_EQ("::p::Outer::Inner", cpp_type->CppType());
+ AidlTypeSpecifier nested_type(AIDL_LOCATION_HERE, "p.Outer.Inner", false, nullptr, "");
+ EXPECT_EQ("::p::Outer::Inner", cpp::CppNameOf(nested_type, typenames_));
}
TEST_F(AidlTest, UnderstandsNativeParcelables) {
@@ -639,24 +648,31 @@
import_paths_.emplace("");
const string input_path = "p/IFoo.aidl";
const string input = "package p; import p.Bar; interface IFoo { }";
-
- // C++ understands C++ specific stuff
- auto cpp_parse_result = Parse(input_path, input, &cpp_types_);
- EXPECT_NE(nullptr, cpp_parse_result);
- auto cpp_type = cpp_types_.FindTypeByCanonicalName("p.Bar");
- ASSERT_NE(nullptr, cpp_type);
- EXPECT_EQ("::p::Bar", cpp_type->CppType());
- set<string> headers;
- cpp_type->GetHeaders(&headers);
- EXPECT_EQ(1u, headers.size());
- EXPECT_EQ(1u, headers.count("baz/header"));
-
- // Java ignores C++ specific stuff
- auto java_parse_result = Parse(input_path, input, &java_types_);
- EXPECT_NE(nullptr, java_parse_result);
- auto java_type = java_types_.FindTypeByCanonicalName("p.Bar");
- ASSERT_NE(nullptr, java_type);
- EXPECT_EQ("p.Bar", java_type->InstantiableName());
+ {
+ // C++ understands C++ specific stuff
+ auto cpp_parse_result = Parse(input_path, input, typenames_, Options::Language::CPP);
+ EXPECT_NE(nullptr, cpp_parse_result);
+ auto pair = typenames_.ResolveTypename("p.Bar");
+ EXPECT_TRUE(pair.second);
+ AidlTypeSpecifier native_type(AIDL_LOCATION_HERE, "p.Bar", false, nullptr, "");
+ native_type.Resolve(typenames_);
+ EXPECT_EQ("::p::Bar", cpp::CppNameOf(native_type, typenames_));
+ set<string> headers;
+ cpp::AddHeaders(native_type, typenames_, headers);
+ EXPECT_EQ(1u, headers.size());
+ EXPECT_EQ(1u, headers.count("baz/header"));
+ }
+ typenames_.Reset();
+ {
+ // Java ignores C++ specific stuff
+ auto java_parse_result = Parse(input_path, input, typenames_, Options::Language::JAVA);
+ EXPECT_NE(nullptr, java_parse_result);
+ auto pair = typenames_.ResolveTypename("p.Bar");
+ EXPECT_TRUE(pair.second);
+ AidlTypeSpecifier native_type(AIDL_LOCATION_HERE, "p.Bar", false, nullptr, "");
+ native_type.Resolve(typenames_);
+ EXPECT_EQ("p.Bar", java::InstantiableJavaSignatureOf(native_type));
+ }
}
TEST_F(AidlTest, WritesCorrectDependencyFile) {
@@ -719,10 +735,11 @@
" List<int, List<String, bool>> foo(); }";
string nested_in_parcelable = "package a; parcelable IData {\n"
" List<int, List<String, bool>> foo;}";
- EXPECT_NE(nullptr, Parse("a/IFoo.aidl", nested_in_iface, &java_types_));
- EXPECT_NE(nullptr, Parse("a/IFoo.aidl", nested_in_iface, &cpp_types_));
- EXPECT_NE(nullptr, Parse("a/IFoo.aidl", nested_in_parcelable, &java_types_));
- EXPECT_NE(nullptr, Parse("a/IFoo.aidl", nested_in_parcelable, &cpp_types_));
+ EXPECT_NE(nullptr, Parse("a/IFoo.aidl", nested_in_iface, typenames_, Options::Language::JAVA));
+ EXPECT_NE(nullptr, Parse("a/IFoo.aidl", nested_in_iface, typenames_, Options::Language::CPP));
+ EXPECT_NE(nullptr, Parse("a/IFoo.aidl", nested_in_parcelable, typenames_,
+Options::Language::JAVA)); EXPECT_NE(nullptr, Parse("a/IFoo.aidl", nested_in_parcelable, typenames_,
+Options::Language::CPP));
}
*/
diff --git a/generate_cpp_unittest.cpp b/generate_cpp_unittest.cpp
index 8b4fc78..092e7cc 100644
--- a/generate_cpp_unittest.cpp
+++ b/generate_cpp_unittest.cpp
@@ -27,7 +27,6 @@
#include "os.h"
#include "tests/fake_io_delegate.h"
#include "tests/test_util.h"
-#include "type_cpp.h"
using ::android::aidl::test::FakeIoDelegate;
using ::android::base::StringPrintf;
@@ -1385,7 +1384,6 @@
protected:
ASTTest(const string& cmdline, const string& file_contents)
: options_(Options::From(cmdline)), file_contents_(file_contents) {
- types_.Init();
}
AidlInterface* ParseSingleInterface() {
@@ -1395,7 +1393,7 @@
vector<string> imported_files;
ImportResolver import_resolver{io_delegate_, options_.InputFiles().at(0), {"."}, {}};
AidlError err = ::android::aidl::internals::load_and_validate_aidl(
- options_.InputFiles().front(), options_, io_delegate_, &types_, &defined_types,
+ options_.InputFiles().front(), options_, io_delegate_, &typenames_, &defined_types,
&imported_files);
if (err != AidlError::OK) {
@@ -1423,7 +1421,7 @@
const Options options_;
const string file_contents_;
FakeIoDelegate io_delegate_;
- TypeNamespace types_;
+ AidlTypenames typenames_;
};
class ComplexTypeInterfaceASTTest : public ASTTest {
@@ -1439,44 +1437,42 @@
TEST_F(ComplexTypeInterfaceASTTest, GeneratesClientHeader) {
AidlInterface* interface = ParseSingleInterface();
ASSERT_NE(interface, nullptr);
- unique_ptr<Document> doc = internals::BuildClientHeader(types_.typenames_, *interface, options_);
+ unique_ptr<Document> doc = internals::BuildClientHeader(typenames_, *interface, options_);
Compare(doc.get(), kExpectedComplexTypeClientHeaderOutput);
}
TEST_F(ComplexTypeInterfaceASTTest, GeneratesClientSource) {
AidlInterface* interface = ParseSingleInterface();
ASSERT_NE(interface, nullptr);
- unique_ptr<Document> doc = internals::BuildClientSource(types_.typenames_, *interface, options_);
+ unique_ptr<Document> doc = internals::BuildClientSource(typenames_, *interface, options_);
Compare(doc.get(), kExpectedComplexTypeClientSourceOutput);
}
TEST_F(ComplexTypeInterfaceASTTest, GeneratesServerHeader) {
AidlInterface* interface = ParseSingleInterface();
ASSERT_NE(interface, nullptr);
- unique_ptr<Document> doc = internals::BuildServerHeader(types_.typenames_, *interface, options_);
+ unique_ptr<Document> doc = internals::BuildServerHeader(typenames_, *interface, options_);
Compare(doc.get(), kExpectedComplexTypeServerHeaderOutput);
}
TEST_F(ComplexTypeInterfaceASTTest, GeneratesServerSource) {
AidlInterface* interface = ParseSingleInterface();
ASSERT_NE(interface, nullptr);
- unique_ptr<Document> doc = internals::BuildServerSource(types_.typenames_, *interface, options_);
+ unique_ptr<Document> doc = internals::BuildServerSource(typenames_, *interface, options_);
Compare(doc.get(), kExpectedComplexTypeServerSourceOutput);
}
TEST_F(ComplexTypeInterfaceASTTest, GeneratesInterfaceHeader) {
AidlInterface* interface = ParseSingleInterface();
ASSERT_NE(interface, nullptr);
- unique_ptr<Document> doc =
- internals::BuildInterfaceHeader(types_.typenames_, *interface, options_);
+ unique_ptr<Document> doc = internals::BuildInterfaceHeader(typenames_, *interface, options_);
Compare(doc.get(), kExpectedComplexTypeInterfaceHeaderOutput);
}
TEST_F(ComplexTypeInterfaceASTTest, GeneratesInterfaceSource) {
AidlInterface* interface = ParseSingleInterface();
ASSERT_NE(interface, nullptr);
- unique_ptr<Document> doc =
- internals::BuildInterfaceSource(types_.typenames_, *interface, options_);
+ unique_ptr<Document> doc = internals::BuildInterfaceSource(typenames_, *interface, options_);
Compare(doc.get(), kExpectedComplexTypeInterfaceSourceOutput);
}
@@ -1492,14 +1488,14 @@
TEST_F(ComplexTypeInterfaceASTTestWithTrace, GeneratesClientSource) {
AidlInterface* interface = ParseSingleInterface();
ASSERT_NE(interface, nullptr);
- unique_ptr<Document> doc = internals::BuildClientSource(types_.typenames_, *interface, options_);
+ unique_ptr<Document> doc = internals::BuildClientSource(typenames_, *interface, options_);
Compare(doc.get(), kExpectedComplexTypeClientWithTraceSourceOutput);
}
TEST_F(ComplexTypeInterfaceASTTestWithTrace, GeneratesServerSource) {
AidlInterface* interface = ParseSingleInterface();
ASSERT_NE(interface, nullptr);
- unique_ptr<Document> doc = internals::BuildServerSource(types_.typenames_, *interface, options_);
+ unique_ptr<Document> doc = internals::BuildServerSource(typenames_, *interface, options_);
Compare(doc.get(), kExpectedComplexTypeServerWithTraceSourceOutput);
}
@@ -1523,8 +1519,7 @@
// Confirm that this is working correctly without I/O problems.
AidlInterface* interface = ParseSingleInterface();
ASSERT_NE(interface, nullptr);
- ASSERT_TRUE(
- GenerateCpp(options_.OutputFile(), options_, types_.typenames_, *interface, io_delegate_));
+ ASSERT_TRUE(GenerateCpp(options_.OutputFile(), options_, typenames_, *interface, io_delegate_));
}
TEST_F(IoErrorHandlingTest, HandlesBadHeaderWrite) {
@@ -1537,8 +1532,7 @@
StringPrintf("%s%c%s", kHeaderDir, OS_PATH_SEPARATOR,
kInterfaceHeaderRelPath);
io_delegate_.AddBrokenFilePath(header_path);
- ASSERT_FALSE(
- GenerateCpp(options_.OutputFile(), options_, types_.typenames_, *interface, io_delegate_));
+ ASSERT_FALSE(GenerateCpp(options_.OutputFile(), options_, typenames_, *interface, io_delegate_));
// We should never attempt to write the C++ file if we fail writing headers.
ASSERT_FALSE(io_delegate_.GetWrittenContents(kOutputPath, nullptr));
// We should remove partial results.
@@ -1552,8 +1546,7 @@
// Simulate issues closing the cpp file.
io_delegate_.AddBrokenFilePath(kOutputPath);
- ASSERT_FALSE(
- GenerateCpp(options_.OutputFile(), options_, types_.typenames_, *interface, io_delegate_));
+ ASSERT_FALSE(GenerateCpp(options_.OutputFile(), options_, typenames_, *interface, io_delegate_));
// We should remove partial results.
ASSERT_TRUE(io_delegate_.PathWasRemoved(kOutputPath));
}
diff --git a/generate_java_binder.cpp b/generate_java_binder.cpp
index af1452b..1581d48 100644
--- a/generate_java_binder.cpp
+++ b/generate_java_binder.cpp
@@ -17,6 +17,7 @@
#include "aidl.h"
#include "aidl_to_java.h"
#include "generate_java.h"
+#include "logging.h"
#include "options.h"
#include <stdio.h>
diff --git a/type_cpp.cpp b/type_cpp.cpp
deleted file mode 100644
index b9bd4b6..0000000
--- a/type_cpp.cpp
+++ /dev/null
@@ -1,502 +0,0 @@
-/*
- * Copyright (C) 2015, The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include "type_cpp.h"
-
-#include <algorithm>
-#include <iostream>
-#include <memory>
-#include <vector>
-
-#include <android-base/stringprintf.h>
-#include <android-base/strings.h>
-
-#include "logging.h"
-
-using std::string;
-using std::vector;
-
-using android::base::Join;
-using android::base::StringPrintf;
-
-namespace android {
-namespace aidl {
-namespace cpp {
-namespace {
-
-const char kNoPackage[] = "";
-const char kNoHeader[] = "";
-const char kNoValidMethod[] = "";
-Type* const kNoArrayType = nullptr;
-Type* const kNoNullableType = nullptr;
-
-class VoidType : public Type {
- public:
- VoidType() : Type(ValidatableType::KIND_BUILT_IN, kNoPackage, "void",
- {}, "void", kNoValidMethod, kNoValidMethod) {}
- ~VoidType() override = default;
- bool CanWriteToParcel() const override { return false; }
-}; // class VoidType
-
-class CppArrayType : public Type {
- public:
- CppArrayType(int kind, // from ValidatableType
- const std::string& package,
- const string& underlying_aidl_type,
- const string& cpp_header,
- const string& underlying_cpp_type,
- const string& underlying_cpp_type_nulllable,
- const string& read_method,
- const string& write_method,
- bool is_nullable,
- const string& src_file_name = "")
- : Type(kind, package,
- underlying_aidl_type + "[]",
- GetHeaders(is_nullable, cpp_header),
- GetCppType(is_nullable, underlying_cpp_type),
- read_method, write_method, kNoArrayType,
- (is_nullable)
- ? kNoNullableType
- // All arrays are nullable.
- : new CppArrayType(kind, package, underlying_aidl_type,
- cpp_header, underlying_cpp_type_nulllable,
- underlying_cpp_type_nulllable,
- read_method, write_method, true),
- src_file_name) {}
-
- private:
- static vector<string> GetHeaders(bool is_nullable, const string& cpp_header) {
- vector<string> result = {"vector"};
- if (is_nullable) {
- result.push_back("memory");
- }
- if (!cpp_header.empty()) {
- result.push_back(cpp_header);
- }
- return result;
- }
-
- static string GetCppType(bool is_nullable,
- const string& underlying_cpp_type) {
- if (is_nullable)
- return StringPrintf("::std::unique_ptr<::std::vector<%s>>",
- underlying_cpp_type.c_str());
- return StringPrintf("::std::vector<%s>",
- underlying_cpp_type.c_str());
- }
-
- DISALLOW_COPY_AND_ASSIGN(CppArrayType);
-}; // class CppArrayType
-
-class PrimitiveType : public Type {
- public:
- PrimitiveType(const std::string& aidl_type,
- const std::string& header,
- const std::string& cpp_type,
- const std::string& read_method,
- const std::string& write_method,
- const std::string& read_array_method,
- const std::string& write_array_method)
- : Type(ValidatableType::KIND_BUILT_IN, kNoPackage, aidl_type, {header},
- cpp_type, read_method, write_method,
- new CppArrayType(ValidatableType::KIND_BUILT_IN, kNoPackage,
- aidl_type, header, cpp_type, cpp_type,
- read_array_method, write_array_method,
- false)) {}
-
- ~PrimitiveType() override = default;
- bool IsCppPrimitive() const override { return true; }
-
- private:
- DISALLOW_COPY_AND_ASSIGN(PrimitiveType);
-}; // class PrimitiveType
-
-// Unfortunately, bytes in Java are signed. However, most C authors would
-// say that a byte is not in fact signed. Compromise: customize this otherwise
-// normal primitive to use signed single bytes, but unsigned byte arrays.
-class ByteType : public Type {
- public:
- ByteType()
- : Type(ValidatableType::KIND_BUILT_IN, kNoPackage, "byte",
- {"cstdint"}, "int8_t", "readByte", "writeByte",
- new CppArrayType(ValidatableType::KIND_BUILT_IN, kNoPackage,
- "byte", "cstdint", "uint8_t", "uint8_t",
- "readByteVector", "writeByteVector",
- false)) {}
-
- ~ByteType() override = default;
- bool IsCppPrimitive() const override { return true; }
-
- private:
- DISALLOW_COPY_AND_ASSIGN(ByteType);
-}; // class PrimitiveType
-
-static string GetCppHeader(const AidlDefinedType& defined_type) {
- vector<string> name = defined_type.GetSplitPackage();
- name.push_back(defined_type.GetName());
- return Join(name, '/') + ".h";
-}
-
-class BinderType : public Type {
- public:
- BinderType(const AidlInterface& interface, const std::string& src_file_name)
- : BinderType(interface, src_file_name,
- new BinderType(interface, src_file_name, kNoNullableType,
- "readNullableStrongBinder"),
- "readStrongBinder") {}
- ~BinderType() override = default;
-
- string WriteCast(const string& val) const override {
- return write_cast_ + "(" + val + ")";
- }
-
- private:
- BinderType(const AidlInterface& interface, const std::string& src_file_name, Type* nullable_type,
- const std::string& read)
- : Type(ValidatableType::KIND_GENERATED, interface.GetPackage(), interface.GetName(),
- {GetCppHeader(interface)}, GetCppName(interface), read, "writeStrongBinder",
- kNoArrayType, nullable_type, src_file_name),
- write_cast_(GetRawCppName(interface) + "::asBinder") {}
-
- static string GetCppName(const AidlInterface& interface) {
- return "::android::sp<" + GetRawCppName(interface) + ">";
- }
-
- static string GetRawCppName(const AidlInterface& interface) {
- vector<string> name = interface.GetSplitPackage();
- string ret;
-
- name.push_back(interface.GetName());
-
- for (const auto& term : name) {
- ret += "::" + term;
- }
-
- return ret;
- }
-
- std::string write_cast_;
-};
-
-class NullableParcelableType : public Type {
- public:
- NullableParcelableType(const AidlParcelable& parcelable, const std::string& cpp_header,
- const std::string& src_file_name)
- : Type(ValidatableType::KIND_PARCELABLE, parcelable.GetPackage(), parcelable.GetName(),
- {cpp_header}, GetCppName(parcelable), "readParcelable", "writeNullableParcelable",
- kNoArrayType, kNoNullableType, src_file_name) {}
- ~NullableParcelableType() override = default;
-
- private:
- static string GetCppName(const AidlParcelable& parcelable) {
- return "::std::unique_ptr<::" + Join(parcelable.GetSplitPackage(), "::") +
- "::" + parcelable.GetCppName() + ">";
- }
-};
-
-class ParcelableType : public Type {
- public:
- ParcelableType(const AidlParcelable& parcelable, const std::string& cpp_header,
- const std::string& src_file_name)
- : Type(ValidatableType::KIND_PARCELABLE, parcelable.GetPackage(), parcelable.GetName(),
- {cpp_header}, GetCppName(parcelable), "readParcelable", "writeParcelable",
- new CppArrayType(ValidatableType::KIND_PARCELABLE, parcelable.GetPackage(),
- parcelable.GetName(), cpp_header, GetCppName(parcelable),
- GetCppName(parcelable), "readParcelableVector",
- "writeParcelableVector", false, src_file_name),
- new NullableParcelableType(parcelable, cpp_header, src_file_name), src_file_name) {}
- ~ParcelableType() override = default;
-
- private:
- static string GetCppName(const AidlParcelable& parcelable) {
- std::vector<std::string> components = parcelable.GetSplitPackage();
- components.push_back(parcelable.GetCppName());
-
- return "::" + Join(components, "::");
- }
-};
-
-class NullableStringListType : public Type {
- public:
- NullableStringListType()
- : Type(ValidatableType::KIND_BUILT_IN,
- "java.util", "List<" + string(kStringCanonicalName) + ">",
- {"utils/String16.h", "memory", "vector"},
- "::std::unique_ptr<::std::vector<std::unique_ptr<::android::String16>>>",
- "readString16Vector", "writeString16Vector") {}
- ~NullableStringListType() override = default;
-
- private:
- DISALLOW_COPY_AND_ASSIGN(NullableStringListType);
-}; // class NullableStringListType
-
-class StringListType : public Type {
- public:
- StringListType()
- : Type(ValidatableType::KIND_BUILT_IN,
- "java.util", "List<" + string(kStringCanonicalName) + ">",
- {"utils/String16.h", "vector"},
- "::std::vector<::android::String16>",
- "readString16Vector", "writeString16Vector",
- kNoArrayType, new NullableStringListType()) {}
- ~StringListType() override = default;
-
- private:
- DISALLOW_COPY_AND_ASSIGN(StringListType);
-}; // class StringListType
-
-class NullableUtf8InCppStringListType : public Type {
- public:
- NullableUtf8InCppStringListType()
- : Type(ValidatableType::KIND_BUILT_IN,
- "java.util", "List<" + string(kUtf8InCppStringCanonicalName) + ">",
- {"memory", "string", "vector"},
- "::std::unique_ptr<::std::vector<std::unique_ptr<::std::string>>>",
- "readUtf8VectorFromUtf16Vector", "writeUtf8VectorAsUtf16Vector") {}
- ~NullableUtf8InCppStringListType() override = default;
-
- private:
- DISALLOW_COPY_AND_ASSIGN(NullableUtf8InCppStringListType);
-}; // class NullableUtf8InCppStringListType
-
-class Utf8InCppStringListType : public Type {
- public:
- Utf8InCppStringListType()
- : Type(ValidatableType::KIND_BUILT_IN,
- "java.util", "List<" + string(kUtf8InCppStringCanonicalName) + ">",
- {"string", "vector"},
- "::std::vector<::std::string>",
- "readUtf8VectorFromUtf16Vector", "writeUtf8VectorAsUtf16Vector",
- kNoArrayType, new NullableUtf8InCppStringListType()) {}
- ~Utf8InCppStringListType() override = default;
-
- private:
- DISALLOW_COPY_AND_ASSIGN(Utf8InCppStringListType);
-}; // class Utf8InCppStringListType
-
-class NullableBinderListType : public Type {
- public:
- NullableBinderListType()
- : Type(ValidatableType::KIND_BUILT_IN, "java.util",
- "List<android.os.IBinder>", {"binder/IBinder.h", "vector"},
- "::std::unique_ptr<::std::vector<::android::sp<::android::IBinder>>>",
- "readStrongBinderVector", "writeStrongBinderVector") {}
- ~NullableBinderListType() override = default;
-
- private:
- DISALLOW_COPY_AND_ASSIGN(NullableBinderListType);
-}; // class NullableBinderListType
-
-class BinderListType : public Type {
- public:
- BinderListType()
- : Type(ValidatableType::KIND_BUILT_IN, "java.util",
- "List<android.os.IBinder>", {"binder/IBinder.h", "vector"},
- "::std::vector<::android::sp<::android::IBinder>>",
- "readStrongBinderVector", "writeStrongBinderVector",
- kNoArrayType, new NullableBinderListType()) {}
- ~BinderListType() override = default;
-
- private:
- DISALLOW_COPY_AND_ASSIGN(BinderListType);
-}; // class BinderListType
-
-} // namespace
-
-Type::Type(int kind,
- const std::string& package,
- const std::string& aidl_type,
- const vector<string>& headers,
- const string& cpp_type,
- const string& read_method,
- const string& write_method,
- Type* array_type,
- Type* nullable_type,
- const string& src_file_name,
- int line)
- : ValidatableType(kind, package, aidl_type, src_file_name, line),
- headers_(headers),
- aidl_type_(aidl_type),
- cpp_type_(cpp_type),
- parcel_read_method_(read_method),
- parcel_write_method_(write_method),
- array_type_(array_type),
- nullable_type_(nullable_type) {}
-
-bool Type::CanWriteToParcel() const { return true; }
-
-void TypeNamespace::Init() {
- Add(std::make_unique<ByteType>());
- Add(std::make_unique<PrimitiveType>("int", "cstdint", "int32_t", "readInt32", "writeInt32",
- "readInt32Vector", "writeInt32Vector"));
- Add(std::make_unique<PrimitiveType>("long", "cstdint", "int64_t", "readInt64", "writeInt64",
- "readInt64Vector", "writeInt64Vector"));
- Add(std::make_unique<PrimitiveType>("float", kNoHeader, "float", "readFloat", "writeFloat",
- "readFloatVector", "writeFloatVector"));
- Add(std::make_unique<PrimitiveType>("double", kNoHeader, "double", "readDouble", "writeDouble",
- "readDoubleVector", "writeDoubleVector"));
- Add(std::make_unique<PrimitiveType>("boolean", kNoHeader, "bool", "readBool", "writeBool",
- "readBoolVector", "writeBoolVector"));
- // C++11 defines the char16_t type as a built in for Unicode characters.
- Add(std::make_unique<PrimitiveType>("char", kNoHeader, "char16_t", "readChar", "writeChar",
- "readCharVector", "writeCharVector"));
-
- Type* string_array_type = new CppArrayType(
- ValidatableType::KIND_BUILT_IN, "java.lang", "String",
- "utils/String16.h", "::android::String16",
- "::std::unique_ptr<::android::String16>", "readString16Vector",
- "writeString16Vector", false);
-
- Type* nullable_string_type =
- new Type(ValidatableType::KIND_BUILT_IN, "java.lang", "String",
- {"memory", "utils/String16.h"}, "::std::unique_ptr<::android::String16>",
- "readString16", "writeString16");
-
- AddAndSetMember(&string_type_,
- std::make_unique<Type>(ValidatableType::KIND_BUILT_IN, "java.lang", "String",
- std::vector<std::string>{"utils/String16.h"},
- "::android::String16", "readString16", "writeString16",
- string_array_type, nullable_string_type));
-
- using ::android::aidl::kAidlReservedTypePackage;
- using ::android::aidl::kUtf8InCppStringClass;
-
- // This type is a Utf16 string in the parcel, but deserializes to
- // a std::string in Utf8 format when we use it in C++.
- Type* cpp_utf8_string_array = new CppArrayType(
- ValidatableType::KIND_BUILT_IN,
- kAidlReservedTypePackage, kUtf8InCppStringClass,
- "string", "::std::string", "::std::unique_ptr<::std::string>",
- "readUtf8VectorFromUtf16Vector", "writeUtf8VectorAsUtf16Vector",
- false);
- Type* nullable_cpp_utf8_string_type =
- new Type(ValidatableType::KIND_BUILT_IN, kAidlReservedTypePackage, kUtf8InCppStringClass,
- std::vector<std::string>{"string", "memory"}, "::std::unique_ptr<::std::string>",
- "readUtf8FromUtf16", "writeUtf8AsUtf16");
- Add(std::make_unique<Type>(ValidatableType::KIND_BUILT_IN, kAidlReservedTypePackage,
- kUtf8InCppStringClass, std::vector<std::string>{"string"},
- "::std::string", "readUtf8FromUtf16", "writeUtf8AsUtf16",
- cpp_utf8_string_array, nullable_cpp_utf8_string_type));
-
- Type* nullable_ibinder = new Type(
- ValidatableType::KIND_BUILT_IN, "android.os", "IBinder",
- {"binder/IBinder.h"}, "::android::sp<::android::IBinder>",
- "readNullableStrongBinder", "writeStrongBinder");
-
- AddAndSetMember(&ibinder_type_,
- std::make_unique<Type>(ValidatableType::KIND_BUILT_IN, "android.os", "IBinder",
- std::vector<std::string>{"binder/IBinder.h"},
- "::android::sp<::android::IBinder>", "readStrongBinder",
- "writeStrongBinder", kNoArrayType, nullable_ibinder));
-
- Add(std::make_unique<BinderListType>());
- Add(std::make_unique<StringListType>());
- Add(std::make_unique<Utf8InCppStringListType>());
-
- Type* fd_vector_type = new CppArrayType(
- ValidatableType::KIND_BUILT_IN, kNoPackage, "FileDescriptor",
- "android-base/unique_fd.h",
- "::android::base::unique_fd", "::android::base::unique_fd",
- "readUniqueFileDescriptorVector", "writeUniqueFileDescriptorVector",
- false);
-
- Add(std::make_unique<Type>(ValidatableType::KIND_BUILT_IN, kNoPackage, "FileDescriptor",
- std::vector<std::string>{"android-base/unique_fd.h"},
- "::android::base::unique_fd", "readUniqueFileDescriptor",
- "writeUniqueFileDescriptor", fd_vector_type));
-
- Type* pfd_vector_type =
- new CppArrayType(ValidatableType::KIND_BUILT_IN, "android.os", "ParcelFileDescriptor",
- "binder/ParcelFileDescriptor.h", "::android::os::ParcelFileDescriptor",
- "::android::os::ParcelFileDescriptor", "readParcelableVector",
- "writeParcelableVector", false);
-
- Type* nullable_pfd_type =
- new Type(ValidatableType::KIND_BUILT_IN, "android.os", "ParcelFileDescriptor",
- std::vector<std::string>{"memory", "binder/ParcelFileDescriptor.h"},
- "::std::unique_ptr<::android::os::ParcelFileDescriptor>", "readParcelable",
- "writeNullableParcelable");
-
- Add(std::make_unique<Type>(ValidatableType::KIND_BUILT_IN, "android.os", "ParcelFileDescriptor",
- std::vector<std::string>{"binder/ParcelFileDescriptor.h"},
- "::android::os::ParcelFileDescriptor", "readParcelable",
- "writeParcelable", pfd_vector_type, nullable_pfd_type));
-
- // Qualify VoidType so we don't get collisions with the VoidType method
- AddAndSetMember(&void_type_, std::make_unique<class VoidType>());
-}
-
-bool TypeNamespace::AddParcelableType(const AidlParcelable& p, const std::string& filename) {
- const std::string cpp_header = p.AsStructuredParcelable() ? GetCppHeader(p) : p.GetCppHeader();
- if (p.IsStableParcelable()) {
- AIDL_ERROR(p) << "@JavaOnlyStableParcelable supports only Java target.";
- return false;
- }
- if (cpp_header.empty()) {
- AIDL_ERROR(p) << "Parcelable " << p.GetCanonicalName() << " has no C++ header defined.";
- return false;
- }
-
- Add(std::make_unique<ParcelableType>(p, cpp_header, filename));
- return true;
-}
-
-bool TypeNamespace::AddBinderType(const AidlInterface& b, const std::string& filename) {
- Add(std::make_unique<BinderType>(b, filename));
- return true;
-}
-
-bool TypeNamespace::AddListType(const std::string& type_name) {
- const Type* contained_type = FindTypeByCanonicalName(type_name);
- if (!contained_type) {
- LOG(ERROR) << "Cannot create List<" << type_name << "> because contained "
- "type cannot be found or is invalid.";
- return false;
- }
- if (contained_type->IsCppPrimitive()) {
- LOG(ERROR) << "Cannot create List<" << type_name << "> because contained "
- "type is a primitive in Java and Java List cannot hold "
- "primitives.";
- return false;
- }
-
- if (contained_type->CanonicalName() == kStringCanonicalName ||
- contained_type->CanonicalName() == kUtf8InCppStringCanonicalName ||
- contained_type == IBinderType()) {
- return true;
- }
-
- // TODO Support lists of parcelables b/23600712
-
- LOG(ERROR) << "aidl-cpp does not yet support List<" << type_name << ">";
- return false;
-}
-
-bool TypeNamespace::AddMapType(const std::string& /* key_type_name */,
- const std::string& /* value_type_name */) {
- // TODO Support list types b/25242025
- LOG(ERROR) << "aidl does not implement support for typed maps!";
- return false;
-}
-
-const ValidatableType* TypeNamespace::GetArgType(const AidlArgument& a, int arg_index,
- const AidlDefinedType& context) const {
- return ::android::aidl::TypeNamespace::GetArgType(a, arg_index, context);
-}
-
-} // namespace cpp
-} // namespace aidl
-} // namespace android
diff --git a/type_cpp.h b/type_cpp.h
deleted file mode 100644
index a27132b..0000000
--- a/type_cpp.h
+++ /dev/null
@@ -1,118 +0,0 @@
-/*
- * Copyright (C) 2015, The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#pragma once
-
-#include <memory>
-#include <string>
-#include <set>
-#include <vector>
-
-#include <android-base/macros.h>
-
-#include "type_namespace.h"
-
-namespace android {
-namespace aidl {
-namespace cpp {
-
-class Type : public ValidatableType {
- public:
- Type(int kind, // from ValidatableType
- const std::string& package,
- const std::string& aidl_type,
- const std::vector<std::string>& header,
- const std::string& cpp_type,
- const std::string& read_method,
- const std::string& write_method,
- Type* array_type = nullptr,
- Type* nullable_type = nullptr,
- const std::string& src_file_name = "",
- int line = -1);
- virtual ~Type() = default;
-
- // overrides of ValidatableType
- bool CanWriteToParcel() const override;
-
- const Type* ArrayType() const override { return array_type_.get(); }
- const Type* NullableType() const override { return nullable_type_.get(); }
- std::string CppType() const { return cpp_type_; }
- const std::string& ReadFromParcelMethod() const {
- return parcel_read_method_;
- }
- const std::string& WriteToParcelMethod() const {
- return parcel_write_method_;
- }
-
- void GetHeaders(std::set<std::string>* headers) const {
- for (const std::string& header : headers_) {
- if (!header.empty()) {
- headers->insert(header);
- }
- }
- }
- virtual bool IsCppPrimitive() const { return false; }
- virtual std::string WriteCast(const std::string& value) const {
- return value;
- }
-
- private:
- // |headers| are the headers we must include to use this type
- const std::vector<std::string> headers_;
- // |aidl_type| is what we find in the yacc generated AST (e.g. "int").
- const std::string aidl_type_;
- // |cpp_type| is what we use in the generated C++ code (e.g. "int32_t").
- const std::string cpp_type_;
- const std::string parcel_read_method_;
- const std::string parcel_write_method_;
-
- const std::unique_ptr<Type> array_type_;
- const std::unique_ptr<Type> nullable_type_;
-
- DISALLOW_COPY_AND_ASSIGN(Type);
-}; // class Type
-
-class TypeNamespace : public ::android::aidl::LanguageTypeNamespace<Type> {
- public:
- TypeNamespace() = default;
- virtual ~TypeNamespace() = default;
-
- void Init() override;
- bool AddParcelableType(const AidlParcelable& p,
- const std::string& filename) override;
- bool AddBinderType(const AidlInterface& b,
- const std::string& filename) override;
- bool AddListType(const std::string& type_name) override;
- bool AddMapType(const std::string& key_type_name,
- const std::string& value_type_name) override;
-
- const ValidatableType* GetArgType(const AidlArgument& a, int arg_index,
- const AidlDefinedType& context) const override;
-
- const Type* VoidType() const { return void_type_; }
- const Type* IBinderType() const { return ibinder_type_; }
-
- private:
- const Type* void_type_ = nullptr;
- const Type* string_type_ = nullptr;
- const Type* ibinder_type_ = nullptr;
-
- DISALLOW_COPY_AND_ASSIGN(TypeNamespace);
-}; // class TypeNamespace
-
-} // namespace cpp
-} // namespace aidl
-} // namespace android
diff --git a/type_cpp_unittest.cpp b/type_cpp_unittest.cpp
deleted file mode 100644
index b098086..0000000
--- a/type_cpp_unittest.cpp
+++ /dev/null
@@ -1,71 +0,0 @@
-/*
- * Copyright (C) 2015, The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include <memory>
-#include <string>
-
-#include <gtest/gtest.h>
-
-#include "type_cpp.h"
-
-using std::string;
-using std::unique_ptr;
-
-namespace android {
-namespace aidl {
-namespace cpp {
-
-namespace {
-
-string kParcelableDotName = "Outer.Inner";
-string kParcelableColonName = "Outer::Inner";
-
-} // namespace
-
-class CppTypeNamespaceTest : public ::testing::Test {
- protected:
- void SetUp() override {
- types_.Init();
- }
- TypeNamespace types_;
-};
-
-TEST_F(CppTypeNamespaceTest, HasSomeBasicTypes) {
- EXPECT_TRUE(types_.HasTypeByCanonicalName("byte"));
- EXPECT_TRUE(types_.HasTypeByCanonicalName("int"));
- EXPECT_TRUE(types_.HasTypeByCanonicalName("long"));
- EXPECT_TRUE(types_.HasTypeByCanonicalName("float"));
- EXPECT_TRUE(types_.HasTypeByCanonicalName("double"));
- EXPECT_TRUE(types_.HasTypeByCanonicalName("boolean"));
- EXPECT_TRUE(types_.HasTypeByCanonicalName("char"));
- EXPECT_TRUE(types_.HasTypeByCanonicalName("String"));
-}
-
-TEST_F(CppTypeNamespaceTest, SupportsListString) {
- EXPECT_TRUE(
- types_.HasTypeByCanonicalName("java.util.List<java.lang.String>"));
-}
-
-TEST_F(CppTypeNamespaceTest, SupportsNestedParcelableClass) {
- unique_ptr<AidlParcelable> parcelable(new AidlParcelable(
- AIDL_LOCATION_HERE, new AidlQualifiedName(AIDL_LOCATION_HERE, kParcelableDotName, ""),
- {"a", "goog"}, ""));
- EXPECT_EQ(parcelable->GetCppName(), kParcelableColonName);
-}
-
-} // namespace cpp
-} // namespace aidl
-} // namespace android
diff --git a/type_java.cpp b/type_java.cpp
deleted file mode 100644
index 5f5f9dc..0000000
--- a/type_java.cpp
+++ /dev/null
@@ -1,351 +0,0 @@
-/*
- * Copyright (C) 2015, The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include "type_java.h"
-
-#include <sys/types.h>
-#include <memory>
-
-#include <android-base/strings.h>
-
-#include "aidl_language.h"
-#include "logging.h"
-
-using std::string;
-
-namespace android {
-namespace aidl {
-namespace java {
-
-// ================================================================
-
-Type::Type(const JavaTypeNamespace* types, const string& name, int kind, bool canWriteToParcel)
- : Type(types, "", name, kind, canWriteToParcel, "", -1) {}
-
-Type::Type(const JavaTypeNamespace* types, const string& package, const string& name, int kind,
- bool canWriteToParcel, const string& declFile, int declLine)
- : ValidatableType(kind, package, name, declFile, declLine),
- m_types(types),
- m_javaType((package.empty()) ? name : package + "." + name),
- m_canWriteToParcel(canWriteToParcel) {}
-
-string Type::InstantiableName() const { return JavaType(); }
-
-Expression* Type::BuildWriteToParcelFlags(int flags) const {
- if (flags == 0) {
- return new LiteralExpression("0");
- }
- if ((flags & PARCELABLE_WRITE_RETURN_VALUE) != 0) {
- return new FieldVariable(m_types->ParcelableInterfaceType()->JavaType(),
- "PARCELABLE_WRITE_RETURN_VALUE");
- }
- return new LiteralExpression("0");
-}
-
-// ================================================================
-
-BasicType::BasicType(const JavaTypeNamespace* types, const string& name,
- const string& marshallParcel, const string& unmarshallParcel,
- const string& writeArrayParcel, const string& createArrayParcel,
- const string& readArrayParcel)
- : Type(types, name, ValidatableType::KIND_BUILT_IN, true),
- m_marshallParcel(marshallParcel),
- m_unmarshallParcel(unmarshallParcel) {
- m_array_type.reset(new BasicArrayType(types, name, writeArrayParcel,
- createArrayParcel, readArrayParcel));
-}
-
-BasicArrayType::BasicArrayType(const JavaTypeNamespace* types, const string& name,
- const string& writeArrayParcel, const string& createArrayParcel,
- const string& readArrayParcel)
- : Type(types, name, ValidatableType::KIND_BUILT_IN, true),
- m_writeArrayParcel(writeArrayParcel),
- m_createArrayParcel(createArrayParcel),
- m_readArrayParcel(readArrayParcel) {}
-
-// ================================================================
-
-FileDescriptorType::FileDescriptorType(const JavaTypeNamespace* types)
- : Type(types, "java.io", "FileDescriptor", ValidatableType::KIND_BUILT_IN, true) {
- m_array_type.reset(new FileDescriptorArrayType(types));
-}
-
-FileDescriptorArrayType::FileDescriptorArrayType(const JavaTypeNamespace* types)
- : Type(types, "java.io", "FileDescriptor", ValidatableType::KIND_BUILT_IN, true) {}
-
-// ================================================================
-
-ParcelFileDescriptorType::ParcelFileDescriptorType(const JavaTypeNamespace* types)
- : Type(types, "android.os", "ParcelFileDescriptor", ValidatableType::KIND_BUILT_IN, true) {
- m_array_type.reset(new ParcelFileDescriptorArrayType(types));
-}
-
-ParcelFileDescriptorArrayType::ParcelFileDescriptorArrayType(const JavaTypeNamespace* types)
- : Type(types, "android.os", "ParcelFileDescriptor", ValidatableType::KIND_BUILT_IN, true) {}
-
-// ================================================================
-
-BooleanType::BooleanType(const JavaTypeNamespace* types)
- : Type(types, "boolean", ValidatableType::KIND_BUILT_IN, true) {
- m_array_type.reset(new BooleanArrayType(types));
-}
-
-BooleanArrayType::BooleanArrayType(const JavaTypeNamespace* types)
- : Type(types, "boolean", ValidatableType::KIND_BUILT_IN, true) {}
-
-// ================================================================
-
-CharType::CharType(const JavaTypeNamespace* types)
- : Type(types, "char", ValidatableType::KIND_BUILT_IN, true) {
- m_array_type.reset(new CharArrayType(types));
-}
-
-CharArrayType::CharArrayType(const JavaTypeNamespace* types)
- : Type(types, "char", ValidatableType::KIND_BUILT_IN, true) {}
-
-// ================================================================
-
-StringType::StringType(const JavaTypeNamespace* types, const std::string& package,
- const std::string& class_name)
- : Type(types, package, class_name, ValidatableType::KIND_BUILT_IN, true) {
- m_array_type.reset(new StringArrayType(types));
-}
-
-StringArrayType::StringArrayType(const JavaTypeNamespace* types)
- : Type(types, "java.lang", "String", ValidatableType::KIND_BUILT_IN, true) {}
-
-// ================================================================
-
-CharSequenceType::CharSequenceType(const JavaTypeNamespace* types)
- : Type(types, "java.lang", "CharSequence", ValidatableType::KIND_BUILT_IN, true) {}
-
-// ================================================================
-
-RemoteExceptionType::RemoteExceptionType(const JavaTypeNamespace* types)
- : Type(types, "android.os", "RemoteException", ValidatableType::KIND_BUILT_IN, false) {}
-
-// ================================================================
-
-RuntimeExceptionType::RuntimeExceptionType(const JavaTypeNamespace* types)
- : Type(types, "java.lang", "RuntimeException", ValidatableType::KIND_BUILT_IN, false) {}
-
-// ================================================================
-
-IBinderType::IBinderType(const JavaTypeNamespace* types)
- : Type(types, "android.os", "IBinder", ValidatableType::KIND_BUILT_IN, true) {
- m_array_type.reset(new IBinderArrayType(types));
-}
-
-IBinderArrayType::IBinderArrayType(const JavaTypeNamespace* types)
- : Type(types, "android.os", "IBinder", ValidatableType::KIND_BUILT_IN, true) {}
-
-// ================================================================
-
-IInterfaceType::IInterfaceType(const JavaTypeNamespace* types)
- : Type(types, "android.os", "IInterface", ValidatableType::KIND_BUILT_IN, false) {}
-
-// ================================================================
-
-BinderType::BinderType(const JavaTypeNamespace* types)
- : Type(types, "android.os", "Binder", ValidatableType::KIND_BUILT_IN, false) {}
-
-// ================================================================
-
-BinderProxyType::BinderProxyType(const JavaTypeNamespace* types)
- : Type(types, "android.os", "BinderProxy", ValidatableType::KIND_BUILT_IN, false) {}
-
-// ================================================================
-
-ParcelType::ParcelType(const JavaTypeNamespace* types)
- : Type(types, "android.os", "Parcel", ValidatableType::KIND_BUILT_IN, false) {}
-
-// ================================================================
-
-ParcelableInterfaceType::ParcelableInterfaceType(const JavaTypeNamespace* types)
- : Type(types, "android.os", "Parcelable", ValidatableType::KIND_BUILT_IN, false) {}
-
-// ================================================================
-
-MapType::MapType(const JavaTypeNamespace* types)
- : Type(types, "java.util", "Map", ValidatableType::KIND_BUILT_IN, true) {}
-
-// ================================================================
-
-ListType::ListType(const JavaTypeNamespace* types)
- : Type(types, "java.util", "List", ValidatableType::KIND_BUILT_IN, true) {}
-
-string ListType::InstantiableName() const { return "java.util.ArrayList"; }
-
-// ================================================================
-
-UserDataType::UserDataType(const JavaTypeNamespace* types, const string& package,
- const string& name, bool builtIn, bool canWriteToParcel,
- const string& declFile, int declLine)
- : Type(types, package, name,
- builtIn ? ValidatableType::KIND_BUILT_IN : ValidatableType::KIND_PARCELABLE,
- canWriteToParcel, declFile, declLine) {
- m_array_type.reset(new UserDataArrayType(types, package, name, builtIn,
- canWriteToParcel, declFile,
- declLine));
-}
-
-UserDataArrayType::UserDataArrayType(const JavaTypeNamespace* types, const string& package,
- const string& name, bool builtIn, bool canWriteToParcel,
- const string& declFile, int declLine)
- : Type(types, package, name,
- builtIn ? ValidatableType::KIND_BUILT_IN : ValidatableType::KIND_PARCELABLE,
- canWriteToParcel, declFile, declLine) {}
-
-// ================================================================
-
-InterfaceType::InterfaceType(const JavaTypeNamespace* types, const string& package,
- const string& name, bool builtIn, const string& declFile, int declLine,
- const Type* stub, const Type* proxy, const Type* defaultImpl)
- : Type(types, package, name,
- builtIn ? ValidatableType::KIND_BUILT_IN : ValidatableType::KIND_INTERFACE, true,
- declFile, declLine),
- stub_(stub),
- proxy_(proxy),
- defaultImpl_(defaultImpl) {}
-
-// ================================================================
-
-GenericListType::GenericListType(const JavaTypeNamespace* types, const Type* contained_type)
- : Type(types, "java.util", "List<" + contained_type->CanonicalName() + ">",
- ValidatableType::KIND_BUILT_IN, true),
- m_contained_type(contained_type) {}
-
-string GenericListType::InstantiableName() const {
- return "java.util.ArrayList<" + m_contained_type->JavaType() + ">";
-}
-
-// ================================================================
-
-ClassLoaderType::ClassLoaderType(const JavaTypeNamespace* types)
- : Type(types, "java.lang", "ClassLoader", ValidatableType::KIND_BUILT_IN, false) {}
-
-// ================================================================
-
-void JavaTypeNamespace::Init() {
- Add(std::make_unique<BasicType>(this, "void", "XXX", "XXX", "XXX", "XXX", "XXX"));
-
- AddAndSetMember(&m_bool_type, std::make_unique<BooleanType>(this));
-
- Add(std::make_unique<BasicType>(this, "byte", "writeByte", "readByte", "writeByteArray",
- "createByteArray", "readByteArray"));
-
- Add(std::make_unique<CharType>(this));
-
- AddAndSetMember(&m_int_type,
- std::make_unique<BasicType>(this, "int", "writeInt", "readInt", "writeIntArray",
- "createIntArray", "readIntArray"));
-
- Add(std::make_unique<BasicType>(this, "long", "writeLong", "readLong", "writeLongArray",
- "createLongArray", "readLongArray"));
-
- Add(std::make_unique<BasicType>(this, "float", "writeFloat", "readFloat", "writeFloatArray",
- "createFloatArray", "readFloatArray"));
-
- Add(std::make_unique<BasicType>(this, "double", "writeDouble", "readDouble", "writeDoubleArray",
- "createDoubleArray", "readDoubleArray"));
-
- AddAndSetMember(&m_string_type, std::make_unique<class StringType>(this, "java.lang", "String"));
- Add(std::make_unique<class StringType>(this, ::android::aidl::kAidlReservedTypePackage,
- ::android::aidl::kUtf8InCppStringClass));
-
- Add(std::make_unique<Type>(this, "java.lang", "Object", ValidatableType::KIND_BUILT_IN, false));
-
- Add(std::make_unique<FileDescriptorType>(this));
-
- Add(std::make_unique<ParcelFileDescriptorType>(this));
-
- Add(std::make_unique<CharSequenceType>(this));
-
- Add(std::make_unique<MapType>(this));
-
- Add(std::make_unique<ListType>(this));
-
- AddAndSetMember(&m_text_utils_type,
- std::make_unique<Type>(this, "android.text", "TextUtils",
- ValidatableType::KIND_BUILT_IN, false));
-
- AddAndSetMember(&m_remote_exception_type, std::make_unique<class RemoteExceptionType>(this));
-
- AddAndSetMember(&m_runtime_exception_type, std::make_unique<class RuntimeExceptionType>(this));
-
- AddAndSetMember(&m_ibinder_type, std::make_unique<class IBinderType>(this));
-
- AddAndSetMember(&m_iinterface_type, std::make_unique<class IInterfaceType>(this));
-
- AddAndSetMember(&m_binder_native_type, std::make_unique<BinderType>(this));
-
- AddAndSetMember(&m_binder_proxy_type, std::make_unique<class BinderProxyType>(this));
-
- AddAndSetMember(&m_parcel_type, std::make_unique<class ParcelType>(this));
-
- AddAndSetMember(&m_parcelable_interface_type,
- std::make_unique<class ParcelableInterfaceType>(this));
-
- AddAndSetMember(&m_context_type, std::make_unique<Type>(this, "android.content", "Context",
- ValidatableType::KIND_BUILT_IN, false));
-
- AddAndSetMember(&m_classloader_type, std::make_unique<class ClassLoaderType>(this));
-}
-
-bool JavaTypeNamespace::AddParcelableType(const AidlParcelable& p,
- const std::string& filename) {
- return Add(
- std::make_unique<UserDataType>(this, p.GetPackage(), p.GetName(), false, true, filename));
-}
-
-bool JavaTypeNamespace::AddBinderType(const AidlInterface& b,
- const std::string& filename) {
- // for interfaces, add the stub, proxy, and interface types.
- auto stub = std::make_unique<Type>(this, b.GetPackage(), b.GetName() + ".Stub",
- ValidatableType::KIND_GENERATED, false, filename);
- auto proxy = std::make_unique<Type>(this, b.GetPackage(), b.GetName() + ".Stub.Proxy",
- ValidatableType::KIND_GENERATED, false, filename);
- auto defaultImpl = std::make_unique<Type>(this, b.GetPackage(), b.GetName() + ".Default",
- ValidatableType::KIND_GENERATED, false, filename);
- auto type = std::make_unique<InterfaceType>(this, b.GetPackage(), b.GetName(), false, filename,
- -1, stub.get(), proxy.get(), defaultImpl.get());
-
- bool success = true;
- success &= Add(std::move(type));
- success &= Add(std::move(stub));
- success &= Add(std::move(proxy));
- success &= Add(std::move(defaultImpl));
- return success;
-}
-
-bool JavaTypeNamespace::AddListType(const std::string& contained_type_name) {
- const Type* contained_type = FindTypeByCanonicalName(contained_type_name);
- if (!contained_type) {
- return false;
- }
- Add(std::make_unique<GenericListType>(this, contained_type));
- return true;
-}
-
-bool JavaTypeNamespace::AddMapType(const string& /*key_type_name*/,
- const string& /*value_type_name*/) {
- LOG(ERROR) << "Don't know how to create a Map<K,V> container.";
- return false;
-}
-
-} // namespace java
-} // namespace aidl
-} // namespace android
diff --git a/type_java.h b/type_java.h
deleted file mode 100644
index dc2c5fb..0000000
--- a/type_java.h
+++ /dev/null
@@ -1,336 +0,0 @@
-/*
- * Copyright (C) 2015, The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#pragma once
-
-#include <string>
-#include <vector>
-
-#include "ast_java.h"
-#include "type_namespace.h"
-
-namespace android {
-namespace aidl {
-namespace java {
-
-class JavaTypeNamespace;
-
-class Type : public ValidatableType {
- public:
- // WriteToParcel flags
- enum { PARCELABLE_WRITE_RETURN_VALUE = 0x0001 };
-
- // defaultValue is by default set to "null" because that is the default value
- // for most of the types like class and array. default values for built-in
- // types like int, double, boolean, etc. are explicitly set via BasicType
- Type(const JavaTypeNamespace* types, const std::string& name, int kind, bool canWriteToParcel);
- Type(const JavaTypeNamespace* types, const std::string& package, const std::string& name,
- int kind, bool canWriteToParcel, const std::string& declFile = "", int declLine = -1);
- virtual ~Type() = default;
-
- bool CanWriteToParcel() const override { return m_canWriteToParcel; }
-
- const ValidatableType* ArrayType() const override { return m_array_type.get(); }
- const ValidatableType* NullableType() const override { return nullptr; }
-
- virtual std::string JavaType() const { return m_javaType; }
- virtual std::string InstantiableName() const;
-
- // The namespace where this type is defined in
- const JavaTypeNamespace* GetTypeNamespace() const { return m_types; }
-
- protected:
- Expression* BuildWriteToParcelFlags(int flags) const;
-
- const JavaTypeNamespace* m_types;
-
- std::unique_ptr<Type> m_array_type;
-
- private:
- Type();
- Type(const Type&);
-
- std::string m_javaType;
- std::string m_declFile;
- bool m_canWriteToParcel;
-};
-
-class BasicArrayType : public Type {
- public:
- BasicArrayType(const JavaTypeNamespace* types, const std::string& name,
- const std::string& writeArrayParcel,
- const std::string& createArrayParcel,
- const std::string& readArrayParcel);
-
- const ValidatableType* NullableType() const override { return this; }
-
- private:
- std::string m_writeArrayParcel;
- std::string m_createArrayParcel;
- std::string m_readArrayParcel;
-};
-
-class BasicType : public Type {
- public:
- BasicType(const JavaTypeNamespace* types, const std::string& name,
- const std::string& marshallParcel, const std::string& unmarshallParcel,
- const std::string& writeArrayParcel, const std::string& createArrayParcel,
- const std::string& readArrayParcel);
-
- private:
- std::string m_marshallParcel;
- std::string m_unmarshallParcel;
-};
-
-class FileDescriptorArrayType : public Type {
- public:
- explicit FileDescriptorArrayType(const JavaTypeNamespace* types);
-
- const ValidatableType* NullableType() const override { return this; }
-};
-
-class FileDescriptorType : public Type {
- public:
- explicit FileDescriptorType(const JavaTypeNamespace* types);
-};
-
-class ParcelFileDescriptorArrayType : public Type {
- public:
- explicit ParcelFileDescriptorArrayType(const JavaTypeNamespace* types);
-
- const ValidatableType* NullableType() const override { return this; }
-};
-
-class ParcelFileDescriptorType : public Type {
- public:
- explicit ParcelFileDescriptorType(const JavaTypeNamespace* types);
-
- const ValidatableType* NullableType() const override { return this; }
-};
-
-class BooleanArrayType : public Type {
- public:
- explicit BooleanArrayType(const JavaTypeNamespace* types);
- const ValidatableType* NullableType() const override { return this; }
-};
-
-class BooleanType : public Type {
- public:
- explicit BooleanType(const JavaTypeNamespace* types);
-};
-
-class CharArrayType : public Type {
- public:
- explicit CharArrayType(const JavaTypeNamespace* types);
- const ValidatableType* NullableType() const override { return this; }
-};
-
-class CharType : public Type {
- public:
- explicit CharType(const JavaTypeNamespace* types);
-};
-
-class StringArrayType : public Type {
- public:
- explicit StringArrayType(const JavaTypeNamespace* types);
-
- const ValidatableType* NullableType() const override { return this; }
-};
-
-class StringType : public Type {
- public:
- StringType(const JavaTypeNamespace* types, const std::string& package,
- const std::string& class_name);
-
- std::string JavaType() const override { return "java.lang.String"; }
- const ValidatableType* NullableType() const override { return this; }
-};
-
-class CharSequenceType : public Type {
- public:
- explicit CharSequenceType(const JavaTypeNamespace* types);
-};
-
-class RemoteExceptionType : public Type {
- public:
- explicit RemoteExceptionType(const JavaTypeNamespace* types);
-};
-
-class RuntimeExceptionType : public Type {
- public:
- explicit RuntimeExceptionType(const JavaTypeNamespace* types);
-};
-
-class IBinderArrayType : public Type {
- public:
- explicit IBinderArrayType(const JavaTypeNamespace* types);
- const ValidatableType* NullableType() const override { return this; }
-};
-
-class IBinderType : public Type {
- public:
- explicit IBinderType(const JavaTypeNamespace* types);
- const ValidatableType* NullableType() const override { return this; }
-};
-
-class IInterfaceType : public Type {
- public:
- explicit IInterfaceType(const JavaTypeNamespace* types);
-};
-
-class BinderType : public Type {
- public:
- explicit BinderType(const JavaTypeNamespace* types);
-};
-
-class BinderProxyType : public Type {
- public:
- explicit BinderProxyType(const JavaTypeNamespace* types);
-};
-
-class ParcelType : public Type {
- public:
- explicit ParcelType(const JavaTypeNamespace* types);
- const ValidatableType* NullableType() const override { return this; }
-};
-
-class ParcelableInterfaceType : public Type {
- public:
- explicit ParcelableInterfaceType(const JavaTypeNamespace* types);
-};
-
-class MapType : public Type {
- public:
- explicit MapType(const JavaTypeNamespace* types);
- const ValidatableType* NullableType() const override { return this; }
-};
-
-class ListType : public Type {
- public:
- explicit ListType(const JavaTypeNamespace* types);
-
- std::string InstantiableName() const override;
- const ValidatableType* NullableType() const override { return this; }
-};
-
-class UserDataArrayType : public Type {
- public:
- UserDataArrayType(const JavaTypeNamespace* types, const std::string& package,
- const std::string& name, bool builtIn,
- bool canWriteToParcel, const std::string& declFile = "",
- int declLine = -1);
-
- const ValidatableType* NullableType() const override { return this; }
-};
-
-class UserDataType : public Type {
- public:
- UserDataType(const JavaTypeNamespace* types, const std::string& package,
- const std::string& name, bool builtIn, bool canWriteToParcel,
- const std::string& declFile = "", int declLine = -1);
-
- const ValidatableType* NullableType() const override { return this; }
-};
-
-class InterfaceType : public Type {
- public:
- InterfaceType(const JavaTypeNamespace* types, const std::string& package, const std::string& name,
- bool builtIn, const std::string& declFile, int declLine, const Type* stub,
- const Type* proxy, const Type* defaultImpl);
-
- const ValidatableType* NullableType() const override { return this; }
- const Type* GetStub() const { return stub_; }
- const Type* GetProxy() const { return proxy_; }
- const Type* GetDefaultImpl() const { return defaultImpl_; }
-
- private:
- const Type* stub_;
- const Type* proxy_;
- const Type* defaultImpl_;
-};
-
-class ClassLoaderType : public Type {
- public:
- explicit ClassLoaderType(const JavaTypeNamespace* types);
-};
-
-class GenericListType : public Type {
- public:
- GenericListType(const JavaTypeNamespace* types, const Type* arg);
-
- std::string InstantiableName() const override;
- std::string JavaType() const override {
- return "java.util.List<" + m_contained_type->JavaType() + ">";
- }
-
- const ValidatableType* NullableType() const override { return this; }
-
- private:
- const Type* m_contained_type;
-};
-
-class JavaTypeNamespace : public LanguageTypeNamespace<Type> {
- public:
- JavaTypeNamespace() = default;
- virtual ~JavaTypeNamespace() = default;
-
- void Init() override;
- bool AddParcelableType(const AidlParcelable& p,
- const std::string& filename) override;
- bool AddBinderType(const AidlInterface& b,
- const std::string& filename) override;
- bool AddListType(const std::string& contained_type_name) override;
- bool AddMapType(const std::string& key_type_name,
- const std::string& value_type_name) override;
-
- const Type* BoolType() const { return m_bool_type; }
- const Type* IntType() const { return m_int_type; }
- const Type* StringType() const { return m_string_type; }
- const Type* TextUtilsType() const { return m_text_utils_type; }
- const Type* RemoteExceptionType() const { return m_remote_exception_type; }
- const Type* RuntimeExceptionType() const { return m_runtime_exception_type; }
- const Type* IBinderType() const { return m_ibinder_type; }
- const Type* IInterfaceType() const { return m_iinterface_type; }
- const Type* BinderNativeType() const { return m_binder_native_type; }
- const Type* BinderProxyType() const { return m_binder_proxy_type; }
- const Type* ParcelType() const { return m_parcel_type; }
- const Type* ParcelableInterfaceType() const {
- return m_parcelable_interface_type;
- }
- const Type* ContextType() const { return m_context_type; }
- const Type* ClassLoaderType() const { return m_classloader_type; }
-
- private:
- const Type* m_bool_type{nullptr};
- const Type* m_int_type{nullptr};
- const Type* m_string_type{nullptr};
- const Type* m_text_utils_type{nullptr};
- const Type* m_remote_exception_type{nullptr};
- const Type* m_runtime_exception_type{nullptr};
- const Type* m_ibinder_type{nullptr};
- const Type* m_iinterface_type{nullptr};
- const Type* m_binder_native_type{nullptr};
- const Type* m_binder_proxy_type{nullptr};
- const Type* m_parcel_type{nullptr};
- const Type* m_parcelable_interface_type{nullptr};
- const Type* m_context_type{nullptr};
- const Type* m_classloader_type{nullptr};
-
- DISALLOW_COPY_AND_ASSIGN(JavaTypeNamespace);
-};
-} // namespace java
-} // namespace aidl
-} // namespace android
diff --git a/type_java_unittest.cpp b/type_java_unittest.cpp
deleted file mode 100644
index 0a5c94b..0000000
--- a/type_java_unittest.cpp
+++ /dev/null
@@ -1,67 +0,0 @@
-/*
- * Copyright (C) 2015, The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include <memory>
-
-#include <gtest/gtest.h>
-
-#include "aidl_language.h"
-#include "type_java.h"
-
-using std::unique_ptr;
-
-namespace android {
-namespace aidl {
-namespace java {
-
-class JavaTypeNamespaceTest : public ::testing::Test {
- protected:
- void SetUp() override {
- types_.Init();
- }
- JavaTypeNamespace types_;
-};
-
-TEST_F(JavaTypeNamespaceTest, HasSomeBasicTypes) {
- EXPECT_TRUE(types_.HasTypeByCanonicalName("void"));
- EXPECT_TRUE(types_.HasTypeByCanonicalName("int"));
- EXPECT_TRUE(types_.HasTypeByCanonicalName("java.lang.String"));
-}
-
-TEST_F(JavaTypeNamespaceTest, ContainerTypeCreation) {
- // We start with no knowledge of parcelables or lists of them.
- EXPECT_FALSE(types_.HasTypeByCanonicalName("Foo"));
- EXPECT_FALSE(types_.HasTypeByCanonicalName("java.util.List<a.goog.Foo>"));
- unique_ptr<AidlParcelable> parcelable(new AidlParcelable(
- AIDL_LOCATION_HERE, new AidlQualifiedName(AIDL_LOCATION_HERE, "Foo", ""), {"a", "goog"}, ""));
- // Add the parcelable type we care about.
- EXPECT_TRUE(types_.AddParcelableType(*parcelable.get(), __FILE__));
- // Now we can find the parcelable type, but not the List of them.
- EXPECT_TRUE(types_.HasTypeByCanonicalName("a.goog.Foo"));
- EXPECT_FALSE(types_.HasTypeByCanonicalName("java.util.List<a.goog.Foo>"));
- // But after we add the list explicitly...
- std::vector<std::unique_ptr<AidlTypeSpecifier>>* type_args =
- new std::vector<std::unique_ptr<AidlTypeSpecifier>>();
- type_args->emplace_back(new AidlTypeSpecifier(AIDL_LOCATION_HERE, "Foo", false, nullptr, ""));
- AidlTypeSpecifier container_type(AIDL_LOCATION_HERE, "List", false, type_args, "");
- EXPECT_TRUE(types_.MaybeAddContainerType(container_type));
- // This should work.
- EXPECT_TRUE(types_.HasTypeByCanonicalName("java.util.List<a.goog.Foo>"));
-}
-
-} // namespace java
-} // namespace aidl
-} // namespace android
diff --git a/type_namespace.cpp b/type_namespace.cpp
deleted file mode 100644
index e8e5a29..0000000
--- a/type_namespace.cpp
+++ /dev/null
@@ -1,164 +0,0 @@
-/*
- * Copyright (C) 2015, The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include "type_namespace.h"
-
-#include <algorithm>
-#include <string>
-#include <vector>
-
-#include "aidl_language.h"
-#include "logging.h"
-
-using android::base::StringPrintf;
-using std::string;
-using std::vector;
-
-namespace android {
-namespace aidl {
-
-// Since packages cannot contain '-' normally, we cannot be asked
-// to create a type that conflicts with these strings.
-const char kAidlReservedTypePackage[] = "aidl-internal";
-const char kUtf8InCppStringClass[] = "Utf8InCppString";
-
-// These *must* match the package and class names above.
-const char kUtf8InCppStringCanonicalName[] = "aidl-internal.Utf8InCppString";
-
-const char kStringCanonicalName[] = "java.lang.String";
-
-const char kUtf8InCppAnnotation[] = "@utfInCpp";
-
-namespace {
-
-bool is_java_keyword(const char* str) {
- static const std::vector<std::string> kJavaKeywords{
- "abstract", "assert", "boolean", "break", "byte",
- "case", "catch", "char", "class", "const",
- "continue", "default", "do", "double", "else",
- "enum", "extends", "final", "finally", "float",
- "for", "goto", "if", "implements", "import",
- "instanceof", "int", "interface", "long", "native",
- "new", "package", "private", "protected", "public",
- "return", "short", "static", "strictfp", "super",
- "switch", "synchronized", "this", "throw", "throws",
- "transient", "try", "void", "volatile", "while",
- "true", "false", "null",
- };
- return std::find(kJavaKeywords.begin(), kJavaKeywords.end(), str) !=
- kJavaKeywords.end();
-}
-
-} // namespace
-
-ValidatableType::ValidatableType(
- int kind, const string& package, const string& type_name,
- const string& decl_file, int decl_line)
- : kind_(kind),
- type_name_(type_name),
- canonical_name_((package.empty()) ? type_name
- : package + "." + type_name),
- origin_file_(decl_file),
- origin_line_(decl_line) {}
-
-string ValidatableType::HumanReadableKind() const {
- switch (Kind()) {
- case ValidatableType::KIND_BUILT_IN:
- return "a built in";
- case ValidatableType::KIND_PARCELABLE:
- return "a parcelable";
- case ValidatableType::KIND_INTERFACE:
- return "an interface";
- case ValidatableType::KIND_GENERATED:
- return "a generated";
- }
- return "unknown";
-}
-
-const ValidatableType* TypeNamespace::GetReturnType(const AidlTypeSpecifier& raw_type,
- const AidlDefinedType& context) const {
- string error_msg;
- const ValidatableType* return_type = GetValidatableType(raw_type, &error_msg, context);
- if (return_type == nullptr) {
- AIDL_ERROR(raw_type) << "Return type " << raw_type.ToString() << ": " << error_msg;
- return nullptr;
- }
-
- return return_type;
-}
-
-bool TypeNamespace::AddDefinedTypes(vector<AidlDefinedType*>& types, const string& filename) {
- bool success = true;
- for (const auto type : types) {
- const AidlInterface* interface = type->AsInterface();
- if (interface != nullptr) {
- success &= AddBinderType(*interface, filename);
- continue;
- }
-
- const AidlParcelable* parcelable = type->AsParcelable();
- if (parcelable != nullptr) {
- success &= AddParcelableType(*parcelable, filename);
- continue;
- }
-
- CHECK(false) << "aidl internal error: unrecognized type";
- }
- return success;
-}
-
-const ValidatableType* TypeNamespace::GetArgType(const AidlArgument& a, int arg_index,
- const AidlDefinedType& context) const {
- string error_prefix =
- StringPrintf("parameter %s (argument %d): ", a.GetName().c_str(), arg_index);
-
- // check the arg type
- string error_msg;
- const ValidatableType* t = GetValidatableType(a.GetType(), &error_msg, context);
- if (t == nullptr) {
- AIDL_ERROR(a) << error_prefix << error_msg;
- return nullptr;
- }
-
- const bool can_be_out = typenames_.CanBeOutParameter(a.GetType());
- if (!a.DirectionWasSpecified() && can_be_out) {
- AIDL_ERROR(a) << error_prefix << "'" << a.GetType().ToString()
- << "' can be an out type, so you must declare it as in, out, or inout.";
- return nullptr;
- }
-
- if (a.GetDirection() != AidlArgument::IN_DIR && !can_be_out) {
- AIDL_ERROR(a) << error_prefix << "'" << a.ToString() << "' can only be an in parameter.";
- return nullptr;
- }
-
- // check that the name doesn't match a keyword
- if (is_java_keyword(a.GetName().c_str())) {
- AIDL_ERROR(a) << error_prefix << "Argument name is a Java or aidl keyword";
- return nullptr;
- }
-
- // Reserve a namespace for internal use
- if (a.GetName().substr(0, 5) == "_aidl") {
- AIDL_ERROR(a) << error_prefix << "Argument name cannot begin with '_aidl'";
- return nullptr;
- }
-
- return t;
-}
-
-} // namespace aidl
-} // namespace android
diff --git a/type_namespace.h b/type_namespace.h
deleted file mode 100644
index a7952fb..0000000
--- a/type_namespace.h
+++ /dev/null
@@ -1,450 +0,0 @@
-/*
- * Copyright (C) 2015, The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#pragma once
-
-#include <memory>
-#include <string>
-
-#include <android-base/macros.h>
-#include <android-base/stringprintf.h>
-#include <android-base/strings.h>
-
-#include "aidl_language.h"
-#include "logging.h"
-
-namespace android {
-namespace aidl {
-
-// Special reserved type names.
-extern const char kAidlReservedTypePackage[];
-extern const char kUtf8InCppStringClass[]; // UTF16 wire format, UTF8 in C++
-
-// Helpful aliases defined to be <kAidlReservedTypePackage>.<class name>
-extern const char kUtf8InCppStringCanonicalName[];
-
-// We sometimes special case this class.
-extern const char kStringCanonicalName[];
-
-// Note that these aren't the strings recognized by the parser, we just keep
-// here for the sake of logging a common string constant.
-extern const char kUtf8InCppAnnotation[];
-
-class ValidatableType {
- public:
- enum {
- KIND_BUILT_IN,
- KIND_PARCELABLE,
- KIND_INTERFACE,
- KIND_GENERATED,
- };
-
- ValidatableType(int kind,
- const std::string& package, const std::string& type_name,
- const std::string& decl_file, int decl_line);
- virtual ~ValidatableType() = default;
-
- virtual bool CanBeArray() const { return ArrayType() != nullptr; }
- virtual bool CanWriteToParcel() const = 0;
-
- virtual const ValidatableType* ArrayType() const = 0;
- virtual const ValidatableType* NullableType() const = 0;
-
- // ShortName() is the class name without a package.
- std::string ShortName() const { return type_name_; }
- // CanonicalName() returns the canonical AIDL type, with packages.
- std::string CanonicalName() const { return canonical_name_; }
-
- int Kind() const { return kind_; }
- std::string HumanReadableKind() const;
- std::string DeclFile() const { return origin_file_; }
- int DeclLine() const { return origin_line_; }
-
- private:
- const int kind_;
- const std::string type_name_;
- const std::string canonical_name_;
- const std::string origin_file_;
- const int origin_line_;
-
- DISALLOW_COPY_AND_ASSIGN(ValidatableType);
-};
-
-class TypeNamespace {
- public:
- // Load the TypeNamespace with built in types. Don't do work in the
- // constructor because many of the useful methods are virtual.
- virtual void Init() = 0;
-
- bool AddDefinedTypes(vector<AidlDefinedType*>& types, const string& filename);
-
- // Load this TypeNamespace with user defined types.
- virtual bool AddParcelableType(const AidlParcelable& p,
- const std::string& filename) = 0;
-
- virtual bool AddBinderType(const AidlInterface& b,
- const std::string& filename) = 0;
- // Add a container type to this namespace. Returns false only
- // on error. Silently discards requests to add non-container types.
- virtual bool MaybeAddContainerType(const AidlTypeSpecifier& aidl_type) = 0;
-
- // Returns true iff this has a type for |import|.
- virtual bool HasImportType(const std::string& import) const = 0;
-
- // Returns a pointer to a type corresponding to |raw_type| or nullptr
- // if this is an invalid return type.
- virtual const ValidatableType* GetReturnType(const AidlTypeSpecifier& raw_type,
- const AidlDefinedType& context) const;
-
- // Returns a pointer to a type corresponding to |a| or nullptr if |a|
- // has an invalid argument type.
- virtual const ValidatableType* GetArgType(const AidlArgument& a, int arg_index,
- const AidlDefinedType& context) const;
-
- // Returns a pointer to a type corresponding to |defined_type|.
- virtual const ValidatableType* GetDefinedType(const AidlDefinedType& defined_type) const = 0;
-
- AidlTypenames typenames_;
-
- protected:
- TypeNamespace() = default;
- virtual ~TypeNamespace() = default;
-
- virtual const ValidatableType* GetValidatableType(const AidlTypeSpecifier& type,
- std::string* error_msg,
- const AidlDefinedType& context) const = 0;
-
- private:
- DISALLOW_COPY_AND_ASSIGN(TypeNamespace);
-};
-
-template<typename T>
-class LanguageTypeNamespace : public TypeNamespace {
- public:
- LanguageTypeNamespace() = default;
- virtual ~LanguageTypeNamespace() = default;
-
- // Get a pointer to an existing type. Searches first by fully-qualified
- // name, and then class name (dropping package qualifiers).
- const T* Find(const AidlTypeSpecifier& aidl_type) const;
-
- // Find a type by its |name|. If |name| refers to a container type (e.g.
- // List<String>) you must turn it into a canonical name first (e.g.
- // java.util.List<java.lang.String>).
- const T* FindTypeByCanonicalName(const std::string& name) const;
- bool HasTypeByCanonicalName(const std::string& type_name) const {
- return FindTypeByCanonicalName(type_name) != nullptr;
- }
- bool HasImportType(const std::string& import) const override {
- return HasTypeByCanonicalName(import);
- }
- const ValidatableType* GetDefinedType(const AidlDefinedType& defined_type) const override {
- return FindTypeByCanonicalName(defined_type.GetCanonicalName());
- }
-
- bool MaybeAddContainerType(const AidlTypeSpecifier& aidl_type) override;
- // We dynamically create container types as we discover them in the parse
- // tree. Returns false if the contained types cannot be canonicalized.
- virtual bool AddListType(const std::string& contained_type_name) = 0;
- virtual bool AddMapType(const std::string& key_type_name,
- const std::string& value_type_name) = 0;
-
- protected:
- bool Add(std::unique_ptr<const T> type);
- void AddAndSetMember(const T** member, std::unique_ptr<const T> type) {
- const T* ptr_value = type.get();
- CHECK(Add(std::move(type)));
- *member = ptr_value;
- }
-
- private:
- // Returns true iff the name can be canonicalized to a container type.
- virtual bool CanonicalizeContainerType(const AidlTypeSpecifier& aidl_type,
- std::vector<std::string>* container_class,
- std::vector<std::string>* contained_type_names) const;
-
- // Returns true if this is a container type, rather than a normal type.
- bool IsContainerType(const std::string& type_name) const;
-
- const ValidatableType* GetValidatableType(const AidlTypeSpecifier& type, std::string* error_msg,
- const AidlDefinedType& context) const override;
-
- std::vector<std::unique_ptr<const T>> types_;
-
- DISALLOW_COPY_AND_ASSIGN(LanguageTypeNamespace);
-}; // class LanguageTypeNamespace
-
-template <typename T>
-bool LanguageTypeNamespace<T>::Add(std::unique_ptr<const T> type) {
- const T* existing = FindTypeByCanonicalName(type->CanonicalName());
- if (!existing) {
- types_.push_back(std::move(type));
- return true;
- }
-
- if (existing->Kind() == ValidatableType::KIND_BUILT_IN) {
- LOG(ERROR) << type->DeclFile() << ":" << type->DeclLine()
- << " attempt to redefine built in class "
- << type->CanonicalName();
- return false;
- }
-
- if (type->Kind() != existing->Kind()) {
- LOG(ERROR) << type->DeclFile() << ":" << type->DeclLine()
- << " attempt to redefine " << type->CanonicalName()
- << " as " << type->HumanReadableKind();
- LOG(ERROR) << existing->DeclFile() << ":" << existing->DeclLine()
- << " previously defined here as "
- << existing->HumanReadableKind();
- return false;
- }
-
- return true;
-}
-
-template <typename T>
-const T* LanguageTypeNamespace<T>::Find(const AidlTypeSpecifier& aidl_type) const {
- using std::string;
- using std::vector;
- using android::base::Join;
- using android::base::Trim;
-
- string name = Trim(aidl_type.IsArray() ? aidl_type.GetName() : aidl_type.ToString());
- if (IsContainerType(name)) {
- vector<string> container_class;
- vector<string> contained_type_names;
- if (!CanonicalizeContainerType(aidl_type, &container_class,
- &contained_type_names)) {
- return nullptr;
- }
- name = Join(container_class, '.') +
- "<" + Join(contained_type_names, ',') + ">";
- }
- // Here, we know that we have the canonical name for this container.
- return FindTypeByCanonicalName(name);
-}
-
-template<typename T>
-const T* LanguageTypeNamespace<T>::FindTypeByCanonicalName(
- const std::string& raw_name) const {
- using android::base::Trim;
-
- std::string name = Trim(raw_name);
- const T* ret = nullptr;
- for (const auto& type : types_) {
- // Always prefer a exact match if possible.
- // This works for primitives and class names qualified with a package.
- if (type->CanonicalName() == name) {
- ret = type.get();
- break;
- }
- // We allow authors to drop packages when refering to a class name.
- if (type->ShortName() == name) {
- ret = type.get();
- }
- }
-
- return ret;
-}
-
-template <typename T>
-bool LanguageTypeNamespace<T>::MaybeAddContainerType(const AidlTypeSpecifier& aidl_type) {
- using android::base::Join;
-
- const std::string& type_name = aidl_type.ToString();
- if (!IsContainerType(type_name)) {
- return true;
- }
-
- std::vector<std::string> container_class;
- std::vector<std::string> contained_type_names;
- if (!CanonicalizeContainerType(aidl_type, &container_class,
- &contained_type_names)) {
- return false;
- }
-
- const std::string canonical_name = Join(container_class, ".") +
- "<" + Join(contained_type_names, ",") + ">";
- if (HasTypeByCanonicalName(canonical_name)) {
- return true;
- }
-
-
- // We only support two types right now and this type is one of them.
- switch (contained_type_names.size()) {
- case 1:
- return AddListType(contained_type_names[0]);
- case 2:
- return AddMapType(contained_type_names[0], contained_type_names[1]);
- default:
- break; // Should never get here, will FATAL below.
- }
-
- LOG(FATAL) << "aidl internal error";
- return false;
-}
-
-template<typename T>
-bool LanguageTypeNamespace<T>::IsContainerType(
- const std::string& type_name) const {
- const size_t opening_brace = type_name.find('<');
- const size_t closing_brace = type_name.find('>');
- if (opening_brace != std::string::npos ||
- closing_brace != std::string::npos) {
- return true; // Neither < nor > appear in normal AIDL types.
- }
- return false;
-}
-
-template <typename T>
-bool LanguageTypeNamespace<T>::CanonicalizeContainerType(
- const AidlTypeSpecifier& aidl_type, std::vector<std::string>* container_class,
- std::vector<std::string>* contained_type_names) const {
- std::string container = aidl_type.GetName();
- std::vector<std::string> args;
- for (auto& type_arg : aidl_type.GetTypeParameters()) {
- if (type_arg->IsGeneric()) {
- // nesting is not allowed yet.
- LOG(ERROR) << "Nested template type '" << aidl_type.ToString() << "'";
- }
-
- std::string type_name = type_arg->ToString();
- // Here, we are relying on FindTypeByCanonicalName to do its best when
- // given a non-canonical name for non-compound type (i.e. not another
- // container).
- const T* arg_type = FindTypeByCanonicalName(type_name);
- if (!arg_type) {
- return false;
- }
-
- // Now get the canonical names for these contained types, remapping them if
- // necessary.
- type_name = arg_type->CanonicalName();
- if (aidl_type.IsUtf8InCpp() && type_name == "java.lang.String") {
- type_name = kUtf8InCppStringCanonicalName;
- }
- args.emplace_back(type_name);
- }
-
- // Map the container name to its canonical form for supported containers.
- if ((container == "List" || container == "java.util.List") &&
- args.size() == 1) {
- *container_class = {"java", "util", "List"};
- *contained_type_names = args;
- return true;
- }
- if ((container == "Map" || container == "java.util.Map") &&
- args.size() == 2) {
- *container_class = {"java", "util", "Map"};
- *contained_type_names = args;
- return true;
- }
-
- LOG(ERROR) << "Unknown find container with name " << container << " and " << args.size()
- << " contained types.";
- return false;
-}
-
-template <typename T>
-const ValidatableType* LanguageTypeNamespace<T>::GetValidatableType(
- const AidlTypeSpecifier& aidl_type, std::string* error_msg,
- const AidlDefinedType& context) const {
- using android::base::StringPrintf;
-
- const ValidatableType* type = Find(aidl_type);
- if (type == nullptr) {
- *error_msg = "unknown type";
- return nullptr;
- }
-
- if (aidl_type.GetName() == "void") {
- if (aidl_type.IsArray()) {
- *error_msg = "void type cannot be an array";
- return nullptr;
- }
- if (aidl_type.IsNullable() || aidl_type.IsUtf8InCpp()) {
- *error_msg = "void type cannot be annotated";
- return nullptr;
- }
- // We have no more special handling for void.
- return type;
- }
-
- bool utf8InCpp = aidl_type.IsUtf8InCpp();
-
- // Strings inside containers get remapped to appropriate utf8 versions when
- // we convert the container name to its canonical form and the look up the
- // type. However, for non-compound types (i.e. those not in a container) we
- // must patch them up here.
- if (IsContainerType(type->CanonicalName())) {
- utf8InCpp = false;
- } else if (aidl_type.GetName() == "String" ||
- aidl_type.GetName() == "java.lang.String") {
- utf8InCpp = utf8InCpp || context.IsUtf8InCpp();
- } else if (utf8InCpp) {
- *error_msg = StringPrintf("type '%s' may not be annotated as %s.", aidl_type.GetName().c_str(),
- kUtf8InCppAnnotation);
- return nullptr;
- }
-
- if (utf8InCpp) {
- type = FindTypeByCanonicalName(kUtf8InCppStringCanonicalName);
- }
-
- // One of our UTF8 transforms made type null
- if (type == nullptr) {
- *error_msg = StringPrintf("%s is unsupported when generating code for this language.",
- kUtf8InCppAnnotation);
- return nullptr;
- }
-
- if (!type->CanWriteToParcel()) {
- *error_msg = "type cannot be marshalled";
- return nullptr;
- }
-
- if (aidl_type.IsArray()) {
- type = type->ArrayType();
- if (!type) {
- *error_msg = StringPrintf("type '%s' cannot be an array",
- aidl_type.GetName().c_str());
- return nullptr;
- }
- }
-
- if (context.IsNullable()) {
- const ValidatableType* nullableType = type->NullableType();
-
- if (nullableType) {
- return nullableType;
- }
- }
-
- if (aidl_type.IsNullable()) {
- type = type->NullableType();
- if (!type) {
- *error_msg = StringPrintf("type '%s%s' cannot be marked as possibly null",
- aidl_type.GetName().c_str(),
- (aidl_type.IsArray()) ? "[]" : "");
- return nullptr;
- }
- }
-
- return type;
-}
-
-} // namespace aidl
-} // namespace android