Qualified Lookup in SwiftLexicalLookup

Here's a quick update for those following along :)

One of the things I discussed with Pavel last week was hooking up the qualified lookup API in SwiftLexicalLookup to the compiler to ensure that both implementations produce the same results. Looking more closely at the compiler, I noticed that the C++ implementation uses two main currency types: DeclNameRef and ValueDecl. Namely, the main qualified-lookup methods I'm trying to test against are:

/// Looks for the requested `member` name in the given nominal type declarations.
bool lookupQualified(ArrayRef<NominalTypeDecl *> types, DeclNameRef member,
                     SourceLoc loc, NLOptions options,
                     SmallVectorImpl<ValueDecl *> &decls) const;

/// Looks for given `member` name in the given `Type`.
bool lookupQualified(Type type, DeclNameRef member,
                     SourceLoc loc, NLOptions options,
                     SmallVectorImpl<ValueDecl *> &decls) const;

Contrary to the SwiftLexicalLookup unqualified-lookup API, the member isn't described using an identifier, but using a DeclNameRef. From my understanding, declaration name reference is basically an optional module selector + an identifier + argument names, e.g., MyModule::myFunc(arg1:arg2:). For now, we don't worry about the module selector, so the advantage of using DeclNameRef over a simple Identifier is that we can match against argument names. Here's a simple example:

struct MyStruct {
  func myFunc(name1: Int) { ... }
  func myFunc(name2: Int) { ... }
}

If we used just the myFunc identifier for lookup, we'd have to return both declarations. However, looking up the myFunc(name1:) name in MyStruct unambiguously returns the first declaration.

Further, ValueDecl gives us a more specific lookup result compared to a generic Decl. A value declaration is any declaration with a name that can evaluate to some value. This includes types, function-like declarations, storage declarations, enum-case elements, and macro declarations (we'll handle macros later):

struct MyType {}  // name "MyType" with type `MyType.Type`
func myFunc() {}  // name "myFunc()" with type `() -> Void`
let binding: Int  // name "binding" with type `Int`
enum A { case a } // name "a" with type `() -> A`

// Macros are special
@freestanding macro file = ...  // name `#file`; unresolved type

Coming back to Swift land, you'll notice that my initial proposal using DeclGroup for lookup is very close to the first C++ lookupQualified overload that accepts nominal types. For now, I aim to open a PR that introduces this nominal-type-based lookup with Swift analogs of the DeclNameRef and ValueDecl types.


For those who want to get into the nitty-gritty, here's what my version of DeclNameRef looks like:

/// A macro reference is either freestanding or attached
enum MacroReference: Hashable {
  case freestanding
  case attached
}
/// The arguments eleemnts used for lookup.
/// A `nil` identifier indicates a `_` or nonexistent label, e.g. 
/// `init(_ param: Int)` or `case myCase(Int)`.
typealias DeclNameArgs = [Identifier?]

indirect enum DeclNameRef: Hashable, CustomDebugStringConvertible {
  /// An identifier (macro or not), possibly with argument names
  case identifier(macro: MacroReference? = nil, identifier: Identifier, args: DeclNameArgs? = nil)

  /// Note that it's illegal to reference a subscript, e.g., `myValue[arg1:arg2:]` 
  /// without actually calling it.
  case `subscript`(args: DeclNameArgs)

  /// An explicit reference to init. E.g. `MyType.init` or `MyType.init(arg:)`
  case `init`(args: DeclNameArgs?)

  /// Only tooling can reference deinits.
  case `deinit`

  /// An unnamed call could refer to an `init` in a static context
  /// or a `callAsFunction` if it's an instance. It could also
  /// refer to `@dynamicallyCallable` or `@dynamicMemberLookup`.
  /// See below.
  case unnamedCall(args: DeclNameArgs)

  /// Other keywords
  case `self`, `Type`, `Protocol`
}

This type is clearly more explicit and more complex than its C++ counterpart, so I'll explain my rationale for all these different cases. The .identifier case is basically the C++ type. The .subscript, .init and .deinit cases are separate because the declarations they reference use keywords rather than user-defined identifiers. The compiler still groups these cases with .identifier but uses a getKind() method to distinguish between them. I separated the cases to capture each one's particular semantics: deinit never takes an argument list, while init and subscript always do (even if empty).

Lastly, unnamedCall is the weirdest one. I think it's best if I demonstrate its intended usage with an example:

struct MyStruct {
  init(initArg: Int) {}
  func callAsFunction(callArg: Int) {}
}

let _ = (MyModule::MyStruct(initArg: 5))(callArg: 0)

Suppose some tool or the type-checker tries to evaluate the expression above. We see the function call expression MyModule::MyStruct(initArg: 5) so we look up MyStruct(initArg:) in MyModule and we get back struct MyStruct of type MyStruct.Type. Since the arguments aren't part of MyStruct, we look up (initArg:) in MyStruct.Type and we get back init(initArg: Int) with type (Int) -> MyStruct. Since the init is applied, we could assume the nested function call produces a value of type MyStruct. Then, we look for (callArg:) in MyStruct and get back our function declaration. In the last two lookups, our DeclNameRef didn't have an identifier, so we would have used unnamedCall. Since the only way for there not to be an identifier is during a non-macro function call, I made unnamedCall its own case.

While there's more to say about ValueDecl, I'm still actively tinkering with its interface, so I'll leave that for a future post.

EDIT: Here's the relevant PR.


As always, I'm looking for suggestions and feedback. Does this seem like a reasonable direction for the qualified-lookup API? Do you envision any problems when it comes to validating these against the compiler's implementation? Thanks for reading this!

1 Like