As described in [the generics design](https://github.com/carbon-language/carbon-lang/blob/trunk/docs/design/generics/details.md#type-structure-of-an-impl-declaration), `impl` declarations are prioritized by type structure. Given two `impl` declarations that match a `type as interface` query, the one that describes the longest prefix of the query without using placeholders is preferred. We implement this by putting all impls in a total order, first by type structure equivalence classes and then by lexical order. When matching an impl, we walk this total order, and stop once we find a match and reach the end of its equivalence class. Equivalence classes are determined by finding the locations of the "holes" (the positions where deduced parameters appear) within the type structure, viewed as a tree. Two impls are in the same equivalence class if their holes are in the same place, and equivalence classes are ordered based on a reverse lexicographical ordering of their holes. Explorer doesn't keep the `Bindings` list for a parameterized type in any particular order, but the type structure rule requires that we consider them in lexical order. In order to support this, we now track an index on the declared parameters of each generic. This is a simple numbering of enclosing generic parameters, both on that generic and on all lexically enclosing generics. Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
The code in this directory defines the AST that represents Carbon code in the
rest of explorer.
The AST is not quite immutable, because some node properties are set during some
phase of static analysis, rather than during parsing. However, AST mutations are
monotonic: once set, a node property cannot be changed. Furthermore, if a
property is set after parsing, its documentation specifies what phase is
responsible for setting it. Certain properties have has_foo() members for
querying whether they are set, but those are for internal use within the phase
that sets them. As a result, you can think of the AST as if it were immutable,
but with certain parts that you can't yet observe, depending on what phase of
compilation you're in.
All node types in the AST are derived from AstNode, and use
LLVM-style RTTI to support
safe down-casting and similar operations. Each abstract class Foo in the
hierarchy has a kind method which returns a enum FooKind that identifies the
concrete type of the object, and a FooKind value can be safely static_casted
to BarKind if that value represents a type that's derived from both Foo and
Bar.
We rely on code generation to help enforce those invariants, so every node type
must be described in ast_rtti.txt. See the documentation in
gen_rtti.py, the code generation script, for details about
the file format and generated code.
The AST class hierarchy is structured in a fairly unsurprising way, with
abstract classes such as Statement and Expression, and concrete classes
representing individual syntactic constructs, such as If for if-statements.
Sometimes it is useful to work with a subset of node types that "cuts across"
the primary class hierarchy. Rather than deal with the pitfalls of multiple
inheritance, we handle these cases using a form of type erasure: we specify a
notional interface that those types conform to, and then define a "view" class
that behaves like a pointer to an instance of that interface. Types declare that
they model an interface Foo by defining a public static member named
ImplementsCarbonFoo. See ValueNodeView for an example of this
pattern.