Nested types know its parent/root/document.

AidlDefinedType::GetRootType() returns its parent type defined in a
document's root. AidlDefinedType::GetDocument() returns the document
where the type is defined.

Bug: 182508839
Test: aidl_unittest
Change-Id: I0e5e5ea304df814ca527dc3d4cdac77414c98b1b
diff --git a/aidl_language.cpp b/aidl_language.cpp
index 694a8aa..1577e26 100644
--- a/aidl_language.cpp
+++ b/aidl_language.cpp
@@ -1114,6 +1114,14 @@
   return AidlCast<AidlDefinedType>(GetEnclosingScope()->GetNode());
 }
 
+const AidlDefinedType* AidlDefinedType::GetRootType() const {
+  const AidlDefinedType* root = this;
+  for (auto parent = root->GetParentType(); parent; parent = parent->GetParentType()) {
+    root = parent;
+  }
+  return root;
+}
+
 // Resolve `name` in the current scope. If not found, delegate to the parent
 std::string AidlDefinedType::ResolveName(const std::string& name) const {
   // For example, in the following, t1's type Baz means x.Foo.Bar.Baz
@@ -1166,11 +1174,11 @@
 }
 
 const AidlDocument& AidlDefinedType::GetDocument() const {
-  // TODO(b/182508839): resolve with nested types when we support nested types
-  auto scope = GetEnclosingScope();
+  const AidlDefinedType* root = GetRootType();
+  auto scope = root->GetEnclosingScope();
   AIDL_FATAL_IF(!scope, this) << "no scope defined.";
   auto doc = AidlCast<AidlDocument>(scope->GetNode());
-  AIDL_FATAL_IF(!doc, this) << "scope is not a document.";
+  AIDL_FATAL_IF(!doc, this) << "root scope is not a document.";
   return *doc;
 }