mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-26 17:21:21 +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.
94 lines
4.1 KiB
C++
94 lines
4.1 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_CHECK_GENERIC_H_
|
|
#define CARBON_TOOLCHAIN_CHECK_GENERIC_H_
|
|
|
|
#include "toolchain/check/context.h"
|
|
#include "toolchain/sem_ir/ids.h"
|
|
|
|
namespace Carbon::Check {
|
|
|
|
// Start processing a declaration or definition that might be a generic entity.
|
|
auto StartGenericDecl(Context& context) -> void;
|
|
|
|
// Start processing a declaration or definition that might be a generic entity.
|
|
auto StartGenericDefinition(Context& context) -> void;
|
|
|
|
// Discard the information about the current generic entity. This should be
|
|
// called instead of `FinishGenericDecl` if the corresponding `Generic` object
|
|
// would not actually be used, or when recovering from an error.
|
|
auto DiscardGenericDecl(Context& context) -> void;
|
|
|
|
// Finish processing a potentially generic declaration and produce a
|
|
// corresponding generic object. Returns SemIR::GenericId::None if this
|
|
// declaration is not actually generic.
|
|
auto BuildGeneric(Context& context, SemIR::InstId decl_id) -> SemIR::GenericId;
|
|
|
|
// Builds eval block for the declaration.
|
|
auto FinishGenericDecl(Context& context, SemIRLoc loc,
|
|
SemIR::GenericId generic_id) -> void;
|
|
|
|
// BuildGeneric() and FinishGenericDecl() combined. Normally you would call this
|
|
// function unless the caller has work to do between the two steps.
|
|
auto BuildGenericDecl(Context& context, SemIR::InstId decl_id)
|
|
-> SemIR::GenericId;
|
|
|
|
// Merge a redeclaration of an entity that might be a generic into the original
|
|
// declaration.
|
|
auto FinishGenericRedecl(Context& context, SemIR::InstId decl_id,
|
|
SemIR::GenericId generic_id) -> void;
|
|
|
|
// Finish processing a potentially generic definition.
|
|
auto FinishGenericDefinition(Context& context, SemIR::GenericId generic_id)
|
|
-> void;
|
|
|
|
// Builds and returns an eval block, given the list of canonical symbolic
|
|
// constants that the instructions in the eval block should produce. This is
|
|
// used when importing a generic.
|
|
auto RebuildGenericEvalBlock(Context& context, SemIR::GenericId generic_id,
|
|
SemIR::GenericInstIndex::Region region,
|
|
llvm::ArrayRef<SemIR::InstId> const_ids)
|
|
-> SemIR::InstBlockId;
|
|
|
|
// Builds a new specific, or finds an existing one if this generic has already
|
|
// been referenced with these arguments. Performs substitution into the
|
|
// declaration, but not the definition, of the generic.
|
|
//
|
|
// `args_id` should be a canonical instruction block referring to constants.
|
|
auto MakeSpecific(Context& context, SemIRLoc loc, SemIR::GenericId generic_id,
|
|
SemIR::InstBlockId args_id) -> SemIR::SpecificId;
|
|
|
|
// Builds a new specific if the given generic is it has a value. Otherwise
|
|
// returns `None`.
|
|
inline auto MakeSpecificIfGeneric(Context& context, SemIRLoc loc,
|
|
SemIR::GenericId generic_id,
|
|
SemIR::InstBlockId args_id)
|
|
-> SemIR::SpecificId {
|
|
return generic_id.has_value()
|
|
? MakeSpecific(context, loc, generic_id, args_id)
|
|
: SemIR::SpecificId::None;
|
|
}
|
|
|
|
// Builds the specific that describes how the generic should refer to itself.
|
|
// For example, for a generic `G(T:! type)`, this is the specific `G(T)`. If
|
|
// `generic_id` is `None`, returns `None`.
|
|
auto MakeSelfSpecific(Context& context, SemIRLoc loc,
|
|
SemIR::GenericId generic_id) -> SemIR::SpecificId;
|
|
|
|
// Attempts to resolve the definition of the given specific, by evaluating the
|
|
// eval block of the corresponding generic and storing a corresponding value
|
|
// block in the specific. Returns false if a definition is not available.
|
|
auto ResolveSpecificDefinition(Context& context, SemIRLoc loc,
|
|
SemIR::SpecificId specific_id) -> bool;
|
|
|
|
// Returns an instruction describing the entity named by the given specific.
|
|
// This is used to name the entity in diagnostics.
|
|
auto GetInstForSpecific(Context& context, SemIR::SpecificId specific_id)
|
|
-> SemIR::InstId;
|
|
|
|
} // namespace Carbon::Check
|
|
|
|
#endif // CARBON_TOOLCHAIN_CHECK_GENERIC_H_
|