Refactor: type resolution done in Scope
Type resolution should be based on "scope". Previously, the compiler
didn't consider its scope and just look it up in the known types. This
caused unnessary complication regarding "preprocessed types" such as
"prefer import over preprocssed".
Now "name resolution" is done in its scope and AidlTypenames don't have
to guess what type it is. (It just looks up fully-qualified names.)
For legacy reasons, preprocessed unstructured parcelable types can still
be resolved with unqualified names.
Bug: 182508839
Test: aidl_unittests
Change-Id: I65b6e2e01bd354b3f0a4027d039a8ef3679ebe6f
diff --git a/aidl_unittest.cpp b/aidl_unittest.cpp
index c59b469..b37bc6e 100644
--- a/aidl_unittest.cpp
+++ b/aidl_unittest.cpp
@@ -706,7 +706,7 @@
EXPECT_TRUE(typenames_.ResolveTypename("another.IBar").is_resolved);
// But if we request just "IBar" we should get our imported one.
AidlTypeSpecifier ambiguous_type(AIDL_LOCATION_HERE, "IBar", false, nullptr, {});
- ambiguous_type.Resolve(typenames_);
+ ambiguous_type.Resolve(typenames_, parse_result);
EXPECT_EQ("one.IBar", ambiguous_type.GetName());
}
@@ -728,7 +728,7 @@
EXPECT_TRUE(typenames_.ResolveTypename("another.IBar").is_resolved);
// But if we request just "IBar" we should get our imported one.
AidlTypeSpecifier ambiguous_type(AIDL_LOCATION_HERE, "IBar", false, nullptr, {});
- ambiguous_type.Resolve(typenames_);
+ ambiguous_type.Resolve(typenames_, parse_result);
EXPECT_EQ("one.IBar", ambiguous_type.GetName());
}
@@ -835,6 +835,18 @@
EXPECT_THAT(GetCapturedStderr(), HasSubstr("Type name can't be qualified"));
}
+TEST_P(AidlTest, PreprocessedCanDeclareJavaStyleBuiltinTypes) {
+ string contents = R"(
+ interface android.os.IBinder;
+ interface android.os.IInterface;
+ parcelable android.os.ParcelFileDescriptor;
+ )";
+ io_delegate_.SetFileContents("path", contents);
+ CaptureStderr();
+ EXPECT_TRUE(Parser::Parse("path", io_delegate_, typenames_, /*is_preprocessed=*/true));
+ EXPECT_THAT(GetCapturedStderr(), "");
+}
+
TEST_P(AidlTest, SupportDeprecated) {
struct TestCase {
std::string output_file;
@@ -1044,6 +1056,17 @@
EXPECT_EQ(expected_bar_stderr, GetCapturedStderr());
}
+TEST_P(AidlTest, ImportingJavaStyleBuiltinTypesIsAllowed) {
+ string contents = R"(
+ import android.os.IBinder;
+ import android.os.IInterface;
+ interface IFoo {
+ void foo(in IBinder b);
+ }
+ )";
+ EXPECT_NE(nullptr, Parse("IFoo.aidl", contents, typenames_, GetLanguage()));
+}
+
TEST_P(AidlTest, StructuredFailOnUnstructuredParcelable) {
const string expected_stderr =
"ERROR: o/WhoKnowsWhat.aidl:1.22-35: o.WhoKnowsWhat is not structured, but this is a "
@@ -1331,7 +1354,7 @@
EXPECT_NE(nullptr, parse_result);
EXPECT_TRUE(typenames_.ResolveTypename("p.Bar").is_resolved);
AidlTypeSpecifier native_type(AIDL_LOCATION_HERE, "p.Bar", false, nullptr, {});
- native_type.Resolve(typenames_);
+ native_type.Resolve(typenames_, parse_result);
EXPECT_EQ("p.Bar", java::InstantiableJavaSignatureOf(native_type, typenames_));
// C++ understands C++ specific stuff
@@ -1813,17 +1836,17 @@
TEST_F(AidlTest, CheckTypeParameterInMapType) {
const string expected_stderr =
"ERROR: p/IFoo.aidl:1.28-31: The type of key in map must be String, but it is 'p.Bar'\n";
- Options options = Options::From("aidl -I p p/IFoo.aidl");
+ Options options = Options::From("aidl -I . p/IFoo.aidl");
io_delegate_.SetFileContents("p/Bar.aidl", "package p; parcelable Bar { String s; }");
io_delegate_.SetFileContents("p/IFoo.aidl",
"package p; interface IFoo {"
- "Map<String, Bar> foo();}");
+ "Map<String, p.Bar> foo();}");
EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_));
io_delegate_.SetFileContents("p/IFoo.aidl",
"package p; interface IFoo {"
- "Map<Bar, Bar> foo();}");
+ "Map<p.Bar, p.Bar> foo();}");
CaptureStderr();
EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_));
EXPECT_EQ(expected_stderr, GetCapturedStderr());
@@ -1851,40 +1874,39 @@
}
TEST_F(AidlTest, UserDefinedUnstructuredGenericParcelableType) {
- Options optionsForParcelable = Options::From("aidl -I p p/Bar.aidl");
+ Options optionsForParcelable = Options::From("aidl -I . p/Bar.aidl");
io_delegate_.SetFileContents("p/Bar.aidl", "package p; parcelable Bar<T, T>;");
CaptureStderr();
EXPECT_NE(0, ::android::aidl::compile_aidl(optionsForParcelable, io_delegate_));
EXPECT_EQ("ERROR: p/Bar.aidl:1.22-26: Every type parameter should be unique.\n",
GetCapturedStderr());
- Options options = Options::From("aidl -I p p/IFoo.aidl");
+ Options options = Options::From("aidl -I . p/IFoo.aidl");
io_delegate_.SetFileContents("p/Bar.aidl", "package p; parcelable Bar;");
io_delegate_.SetFileContents("p/IFoo.aidl",
"package p; interface IFoo {"
- "Bar<String, String> foo();}");
+ "p.Bar<String, String> foo();}");
CaptureStderr();
EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_));
- EXPECT_EQ("ERROR: p/IFoo.aidl:1.28-31: p.Bar is not a generic type.\n", GetCapturedStderr());
+ EXPECT_THAT(GetCapturedStderr(), HasSubstr("p.Bar is not a generic type"));
io_delegate_.SetFileContents("p/Bar.aidl", "package p; parcelable Bar<T>;");
CaptureStderr();
EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_));
- EXPECT_EQ("ERROR: p/IFoo.aidl:1.28-31: p.Bar must have 1 type parameters, but got 2\n",
- GetCapturedStderr());
+ EXPECT_THAT(GetCapturedStderr(), HasSubstr("p.Bar must have 1 type parameters, but got 2"));
io_delegate_.SetFileContents("p/Bar.aidl", "package p; parcelable Bar<T, V>;");
EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_));
io_delegate_.SetFileContents("p/IFoo.aidl",
"package p; interface IFoo {"
- "Bar<String, ParcelFileDescriptor> foo();}");
+ "p.Bar<String, ParcelFileDescriptor> foo();}");
EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_));
io_delegate_.SetFileContents("p/IFoo.aidl",
"package p; interface IFoo {"
- "Bar<int, long> foo();}");
+ "p.Bar<int, long> foo();}");
io_delegate_.SetFileContents("p/IFoo.aidl",
"package p; interface IFoo {"
- "Bar<int[], long[]> foo();}");
+ "p.Bar<int[], long[]> foo();}");
EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_));
}