Files
carbon-lang/toolchain/check/generic.h
T
Jon Ross-Perkins 6cc5d7ed2a Add an EnumMaskBase type (#6053)
This is a bit of an experiment to see if there's a reasonable way to
write a shared enum type, rather than writing per-case wrappers for
things like `HasTypeQualifiers` or the printing. I think it's a bit
borderline complexity right now, but I'm not sure I can reduce it much
further.

This changes from things like `Internal::EnumClassName##RawEnum` to
`Internal::EnumClassName##Data::RawEnum` so that the enum entries can
have back references to bit shifts without needing to know the
containing type name. Because I'm trying to reduce duplication between
mask and non-mask enums, I did this to non-mask enums too.

This was motivated by #6035 adding another enum mask (which will grow
more entries, and is intended to switch if this is accepted), but I'm
not using that PR as a base here because I didn't want the merge
dependency.
2025-09-12 18:04:10 +00:00

131 lines
5.9 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 "common/enum_mask_base.h"
#include "toolchain/check/context.h"
#include "toolchain/sem_ir/entity_with_params_base.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, SemIR::GenericId generic_id)
-> void;
#define CARBON_DEPENDENT_INST_KIND(X) \
/* The type of the instruction depends on a checked generic parameter. */ \
X(SymbolicType) \
/* The constant value of the instruction depends on a checked generic \
* parameter. */ \
X(SymbolicConstant) \
X(Template)
CARBON_DEFINE_RAW_ENUM_MASK(DependentInstKind, uint8_t) {
CARBON_DEPENDENT_INST_KIND(CARBON_RAW_ENUM_MASK_ENUMERATOR)
};
// Represents a set of keyword modifiers, using a separate bit per modifier.
class DependentInstKind : public CARBON_ENUM_MASK_BASE(DependentInstKind) {
public:
CARBON_DEPENDENT_INST_KIND(CARBON_ENUM_MASK_CONSTANT_DECL)
};
#define CARBON_DEPENDENT_INST_KIND_WITH_TYPE(X) \
CARBON_ENUM_MASK_CONSTANT_DEFINITION(DependentInstKind, X)
CARBON_DEPENDENT_INST_KIND(CARBON_DEPENDENT_INST_KIND_WITH_TYPE)
#undef CARBON_DEPENDENT_INST_KIND_WITH_TYPE
// An instruction that depends on a generic parameter in some way.
struct DependentInst {
SemIR::InstId inst_id;
DependentInstKind kind;
};
// Attach a dependent instruction to the current generic, updating its type and
// constant value as necessary.
auto AttachDependentInstToCurrentGeneric(Context& context,
DependentInst dependent_inst) -> 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, SemIR::LocId loc_id,
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::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 with a given argument list, 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.
auto MakeSpecific(Context& context, SemIR::LocId loc_id,
SemIR::GenericId generic_id,
llvm::ArrayRef<SemIR::InstId> args) -> SemIR::SpecificId;
// Builds a new specific or finds an existing one in the case where the argument
// list has already been converted into an instruction block. `args_id` should
// be a canonical instruction block referring to constants.
auto MakeSpecific(Context& context, SemIR::LocId loc_id,
SemIR::GenericId generic_id, SemIR::InstBlockId args_id)
-> SemIR::SpecificId;
// 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, SemIR::LocId loc_id,
SemIR::GenericId generic_id) -> SemIR::SpecificId;
// Resolve the declaration of the given specific, by evaluating the eval block
// of the corresponding generic and storing a corresponding value block in the
// specific.
auto ResolveSpecificDecl(Context& context, SemIR::LocId loc_id,
SemIR::SpecificId specific_id) -> void;
// 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, SemIR::LocId loc_id,
SemIR::SpecificId specific_id) -> bool;
// Diagnoses if an entity has implicit parameters, indicating it's generic, but
// is missing explicit parameters.
auto DiagnoseIfGenericMissingExplicitParameters(
Context& context, SemIR::EntityWithParamsBase& entity_base) -> void;
} // namespace Carbon::Check
#endif // CARBON_TOOLCHAIN_CHECK_GENERIC_H_