mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-15 22:28:14 +00:00
This allows a generic class to inherit from a virtual C++ class as long as the subclass doesn't override any methods. See toolchain/lower/testdata/interop/cpp/class/import/inherit.carbon for a working example. The TODO in `CheckCompleteClassType` is now in `CarbonExternalASTSource::CompleteType`, and only triggers if virtual function thunks need to be defined. A fix in `FileContext::BuildVtable` is also needed: when looking up the clang decl associated with the vtable, if the class is generic then the `ClassTemplateSpecializationDecl` associated with the specific is needed. This required adding `ConstantStore::Get`, since only `const` access is allowed during lowering.
98 lines
3.7 KiB
C++
98 lines
3.7 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
|
|
|
|
#include "toolchain/sem_ir/constant.h"
|
|
|
|
#include "toolchain/base/value_store_impl.h"
|
|
#include "toolchain/sem_ir/file.h"
|
|
|
|
namespace Carbon::SemIR {
|
|
|
|
auto ConstantStore::GetOrAdd(Inst inst, ConstantDependence dependence)
|
|
-> ConstantId {
|
|
auto result = map_.Insert(inst, [&] {
|
|
auto inst_id = sem_ir_->insts().AddInNoBlock(LocIdAndInst::NoLoc(inst));
|
|
ConstantId const_id = ConstantId::None;
|
|
if (dependence == ConstantDependence::None) {
|
|
const_id = ConstantId::ForConcreteConstant(inst_id);
|
|
} else {
|
|
// The instruction in the constants store is an abstract symbolic
|
|
// constant, not associated with any particular generic.
|
|
SymbolicConstant symbolic_constant = {.inst_id = inst_id,
|
|
.generic_id = GenericId::None,
|
|
.index = GenericInstIndex::None,
|
|
.dependence = dependence};
|
|
const_id =
|
|
sem_ir_->constant_values().AddSymbolicConstant(symbolic_constant);
|
|
}
|
|
sem_ir_->constant_values().Set(inst_id, const_id);
|
|
constants_.push_back(inst_id);
|
|
return const_id;
|
|
});
|
|
CARBON_CHECK(result.value() != ConstantId::None);
|
|
CARBON_CHECK(
|
|
result.value().is_symbolic() == (dependence != ConstantDependence::None),
|
|
"Constant {0} registered as both symbolic and concrete constant.", inst);
|
|
return result.value();
|
|
}
|
|
|
|
auto ConstantStore::Get(Inst inst) const -> ConstantId {
|
|
auto result = map_.Lookup(inst);
|
|
CARBON_CHECK(result);
|
|
return result.value();
|
|
}
|
|
|
|
auto GetInstWithConstantValue(const File& file, ConstantId const_id) -> InstId {
|
|
if (!const_id.has_value() || !const_id.is_constant()) {
|
|
return InstId::None;
|
|
}
|
|
|
|
// For concrete constants, the corresponding instruction has the desired
|
|
// constant value.
|
|
if (!const_id.is_symbolic()) {
|
|
return file.constant_values().GetInstId(const_id);
|
|
}
|
|
|
|
// For unattached symbolic constants, the corresponding instruction has the
|
|
// desired constant value.
|
|
const auto& symbolic_const =
|
|
file.constant_values().GetSymbolicConstant(const_id);
|
|
if (!symbolic_const.generic_id.has_value()) {
|
|
return file.constant_values().GetInstId(const_id);
|
|
}
|
|
|
|
// For attached symbolic constants, pick the corresponding instruction out of
|
|
// the eval block for the generic.
|
|
const auto& generic = file.generics().Get(symbolic_const.generic_id);
|
|
auto block = generic.GetEvalBlock(symbolic_const.index.region());
|
|
return file.inst_blocks().Get(block)[symbolic_const.index.index()];
|
|
}
|
|
|
|
auto ConstantValueStore::OutputYaml(bool include_singletons) const
|
|
-> Yaml::OutputMapping {
|
|
return Yaml::OutputMapping([&, include_singletons](
|
|
Yaml::OutputMapping::Map map) {
|
|
map.Add("values", Yaml::OutputMapping([&](Yaml::OutputMapping::Map map) {
|
|
for (auto [id, value] : values_.enumerate()) {
|
|
if (!include_singletons && IsSingletonInstId(id)) {
|
|
continue;
|
|
}
|
|
if (!value.has_value() || value.is_constant()) {
|
|
map.Add(PrintToString(id), Yaml::OutputScalar(value));
|
|
}
|
|
}
|
|
}));
|
|
map.Add("symbolic_constants", symbolic_constants_.OutputYaml());
|
|
});
|
|
}
|
|
|
|
} // namespace Carbon::SemIR
|
|
|
|
namespace Carbon {
|
|
template class ValueStore<SemIR::InstId, SemIR::ConstantId,
|
|
Tag<SemIR::CheckIRId>>;
|
|
template class ValueStore<SemIR::ConstantId::SymbolicId,
|
|
SemIR::SymbolicConstant, Tag<SemIR::CheckIRId>>;
|
|
} // namespace Carbon
|