mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-24 14:32:42 +00:00
Add a full entity representation for associated constants, and build a `Generic` object for them. This `Generic` is parameterized by the enclosing `Self` type, allowing the use of `Self` within the type of the associated constant to be supported. When performing impl lookup for an associated constant, produce the type with the provided self type substituted for its `Self` along with any generic parameters of the interface. Split the handling of associated constant declarations into two parts, corresponding to the code before the `=`, and the code between the `=` and `;` (if any). The former goes into the generic declaration region; the latter into the generic definition region. This prepares us to handle the default value for an associated constant, but for now we're just storing the information and not actually using it. Remove the entity type field from `assoc_entity_type`, because it's almost unused and is an attractive nuisance -- it must necessarily be a type in the generic scope of the associated constant rather than in the scope of the instruction (because there is no `Self` anywhere else), which means that it's hard to substitute into or derive meaning from. See `toolchain/check/testdata/impl/assoc_const_self.carbon` for tests of the new functionality; these used to cause the toolchain to crash.
54 lines
1.5 KiB
C++
54 lines
1.5 KiB
C++
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
|
|
// Exceptions. See /LICENSE for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
#ifndef CARBON_TOOLCHAIN_SEM_IR_ASSOCIATED_CONSTANT_H_
|
|
#define CARBON_TOOLCHAIN_SEM_IR_ASSOCIATED_CONSTANT_H_
|
|
|
|
#include "common/ostream.h"
|
|
#include "toolchain/sem_ir/ids.h"
|
|
|
|
namespace Carbon::SemIR {
|
|
|
|
// An associated constant entity. For example:
|
|
//
|
|
// interface I {
|
|
// let AssocConst:! type;
|
|
// }
|
|
//
|
|
// TODO: This overlaps a lot with EntityName and EntityWithParamsBase.
|
|
// Investigate ways of factoring out the common parts.
|
|
struct AssociatedConstant : public Printable<AssociatedConstant> {
|
|
auto Print(llvm::raw_ostream& out) const -> void {
|
|
out << "{"
|
|
<< "name: " << name_id << ", parent_scope: " << parent_scope_id << "}";
|
|
}
|
|
|
|
// The following fields are set at the `:!` binding, when the
|
|
// `AssociatedConstant` is created.
|
|
|
|
// The entity's name.
|
|
NameId name_id;
|
|
|
|
// The parent scope.
|
|
NameScopeId parent_scope_id;
|
|
|
|
// The declaration of this associated constant.
|
|
InstId decl_id;
|
|
|
|
// The following fields are set at the `=` or `;`.
|
|
|
|
// Information about the generic. An associated constant is always
|
|
// parameterized by `Self`, so this is always present.
|
|
GenericId generic_id;
|
|
|
|
// The following fields are set at the `;`.
|
|
|
|
// The default value of the constant.
|
|
InstId default_value_id = InstId::None;
|
|
};
|
|
|
|
} // namespace Carbon::SemIR
|
|
|
|
#endif // CARBON_TOOLCHAIN_SEM_IR_ASSOCIATED_CONSTANT_H_
|