How does the compiler look for opaque types?

I was browsing the Swift source code (in C++ using the LLVM interface libraries). I was reading the file about recognizing type expressions and another file about recognizing value expressions. I was thinking about trying to add a type construction that involves an opaque type. I was ready to think about parsing a "some" followed by another type expression, but I got worried about what if the opaque type is hidden behind a type alias. I recently tried a (macOS) playground, and it doesn't allow making an opaque type into an alias.

That's good from a user viewpoint. But now I'm wondering how opaque types are identified. Does each type expression that allows an opaque type within it search for a "some" followed by another type expression? Or is there a common code all those experiences share? And if it's shared code (I hope so), where is it?

In some sense yes. (I don't think they are allowed in nested positions though.). You can take a look at this method for more details.

OpaqueTypeDecl *
OpaqueResultTypeRequest::evaluate(Evaluator &evaluator,
                                  ValueDecl *originatingDecl) const 

It has one of the calls to OpaqueTypeArchetypeType::get which is a factory method.

You can also take a look at getOpaqueResultTypeDecl() and it's callers. (Xcode trips over at displaying the call hierarchy for OpaqueResultTypeRequest::evaluate, so you might think nothing is calling it.)

Terms of Service

Privacy Policy

Cookie Policy