eval_inst.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "toolchain/check/eval_inst.h"
  5. #include <variant>
  6. #include "toolchain/check/action.h"
  7. #include "toolchain/check/facet_type.h"
  8. #include "toolchain/check/generic.h"
  9. #include "toolchain/check/impl_lookup.h"
  10. #include "toolchain/check/import_ref.h"
  11. #include "toolchain/check/type.h"
  12. #include "toolchain/check/type_completion.h"
  13. #include "toolchain/sem_ir/ids.h"
  14. #include "toolchain/sem_ir/typed_insts.h"
  15. namespace Carbon::Check {
  16. // When calling from eval to various Check functions, we need the actual LocId.
  17. // This allows us to unwrap the SemIRLoc to do so.
  18. //
  19. // TODO: Decide whether to refactor calls everywhere to accept `SemIRLoc`, or
  20. // fold `SemIRLoc` into `LocId`. Either way, we would like eval to call other
  21. // code without unwrapping `SemIRLoc`.
  22. class UnwrapSemIRLoc {
  23. public:
  24. auto operator()(Context& context, SemIRLoc loc) -> SemIR::LocId {
  25. if (loc.is_inst_id_) {
  26. if (loc.inst_id_.has_value()) {
  27. return context.insts().GetLocId(loc.inst_id_);
  28. } else {
  29. return SemIR::LocId::None;
  30. }
  31. } else {
  32. return loc.loc_id_;
  33. }
  34. }
  35. };
  36. // Performs an access into an aggregate, retrieving the specified element.
  37. static auto PerformAggregateAccess(Context& context, SemIR::Inst inst)
  38. -> ConstantEvalResult {
  39. auto access_inst = inst.As<SemIR::AnyAggregateAccess>();
  40. if (auto aggregate = context.insts().TryGetAs<SemIR::AnyAggregateValue>(
  41. access_inst.aggregate_id)) {
  42. auto elements = context.inst_blocks().Get(aggregate->elements_id);
  43. auto index = static_cast<size_t>(access_inst.index.index);
  44. CARBON_CHECK(index < elements.size(), "Access out of bounds.");
  45. // `Phase` is not used here. If this element is a concrete constant, then
  46. // so is the result of indexing, even if the aggregate also contains a
  47. // symbolic context.
  48. return ConstantEvalResult::Existing(
  49. context.constant_values().Get(elements[index]));
  50. }
  51. return ConstantEvalResult::NewSamePhase(inst);
  52. }
  53. auto EvalConstantInst(Context& /*context*/, SemIRLoc /*loc*/,
  54. SemIR::ArrayInit inst) -> ConstantEvalResult {
  55. // TODO: Add an `ArrayValue` to represent a constant array object
  56. // representation instead of using a `TupleValue`.
  57. return ConstantEvalResult::NewSamePhase(
  58. SemIR::TupleValue{.type_id = inst.type_id, .elements_id = inst.inits_id});
  59. }
  60. auto EvalConstantInst(Context& context, SemIRLoc loc, SemIR::ArrayType inst)
  61. -> ConstantEvalResult {
  62. auto bound_inst = context.insts().Get(inst.bound_id);
  63. auto int_bound = bound_inst.TryAs<SemIR::IntValue>();
  64. if (!int_bound) {
  65. CARBON_CHECK(context.constant_values().Get(inst.bound_id).is_symbolic(),
  66. "Unexpected inst {0} for template constant int", bound_inst);
  67. return ConstantEvalResult::NewSamePhase(inst);
  68. }
  69. // TODO: We should check that the size of the resulting array type
  70. // fits in 64 bits, not just that the bound does. Should we use a
  71. // 32-bit limit for 32-bit targets?
  72. const auto& bound_val = context.ints().Get(int_bound->int_id);
  73. if (context.types().IsSignedInt(int_bound->type_id) &&
  74. bound_val.isNegative()) {
  75. CARBON_DIAGNOSTIC(ArrayBoundNegative, Error,
  76. "array bound of {0} is negative", TypedInt);
  77. context.emitter().Emit(loc, ArrayBoundNegative,
  78. {.type = int_bound->type_id, .value = bound_val});
  79. return ConstantEvalResult::Error;
  80. }
  81. if (bound_val.getActiveBits() > 64) {
  82. CARBON_DIAGNOSTIC(ArrayBoundTooLarge, Error,
  83. "array bound of {0} is too large", TypedInt);
  84. context.emitter().Emit(loc, ArrayBoundTooLarge,
  85. {.type = int_bound->type_id, .value = bound_val});
  86. return ConstantEvalResult::Error;
  87. }
  88. return ConstantEvalResult::NewSamePhase(inst);
  89. }
  90. auto EvalConstantInst(Context& context, SemIRLoc /*loc*/,
  91. SemIR::AsCompatible inst) -> ConstantEvalResult {
  92. // AsCompatible changes the type of the source instruction; its constant
  93. // value, if there is one, needs to be modified to be of the same type.
  94. auto value_id = context.constant_values().Get(inst.source_id);
  95. CARBON_CHECK(value_id.is_constant());
  96. auto value_inst =
  97. context.insts().Get(context.constant_values().GetInstId(value_id));
  98. value_inst.SetType(inst.type_id);
  99. return ConstantEvalResult::NewAnyPhase(value_inst);
  100. }
  101. auto EvalConstantInst(Context& context, SemIRLoc /*loc*/, SemIR::BindAlias inst)
  102. -> ConstantEvalResult {
  103. // An alias evaluates to the value it's bound to.
  104. return ConstantEvalResult::Existing(
  105. context.constant_values().Get(inst.value_id));
  106. }
  107. auto EvalConstantInst(Context& /*context*/, SemIRLoc /*loc*/,
  108. SemIR::BindValue /*inst*/) -> ConstantEvalResult {
  109. // TODO: Handle this once we've decided how to represent constant values of
  110. // reference expressions.
  111. return ConstantEvalResult::TODO;
  112. }
  113. auto EvalConstantInst(Context& context, SemIRLoc /*loc*/,
  114. SemIR::ClassElementAccess inst) -> ConstantEvalResult {
  115. return PerformAggregateAccess(context, inst);
  116. }
  117. auto EvalConstantInst(Context& context, SemIRLoc /*loc*/, SemIR::ClassDecl inst)
  118. -> ConstantEvalResult {
  119. // If the class has generic parameters, we don't produce a class type, but a
  120. // callable whose return value is a class type.
  121. if (context.classes().Get(inst.class_id).has_parameters()) {
  122. return ConstantEvalResult::NewSamePhase(SemIR::StructValue{
  123. .type_id = inst.type_id, .elements_id = SemIR::InstBlockId::Empty});
  124. }
  125. // A non-generic class declaration evaluates to the class type.
  126. return ConstantEvalResult::NewSamePhase(
  127. SemIR::ClassType{.type_id = SemIR::TypeType::SingletonTypeId,
  128. .class_id = inst.class_id,
  129. .specific_id = SemIR::SpecificId::None});
  130. }
  131. auto EvalConstantInst(Context& /*context*/, SemIRLoc /*loc*/,
  132. SemIR::ClassInit inst) -> ConstantEvalResult {
  133. // TODO: Add a `ClassValue` to represent a constant class object
  134. // representation instead of using a `StructValue`.
  135. return ConstantEvalResult::NewSamePhase(SemIR::StructValue{
  136. .type_id = inst.type_id, .elements_id = inst.elements_id});
  137. }
  138. auto EvalConstantInst(Context& context, SemIRLoc /*loc*/, SemIR::ConstType inst)
  139. -> ConstantEvalResult {
  140. // `const (const T)` evaluates to `const T`.
  141. if (context.types().Is<SemIR::ConstType>(inst.inner_id)) {
  142. return ConstantEvalResult::Existing(
  143. context.types().GetConstantId(inst.inner_id));
  144. }
  145. // Otherwise, `const T` evaluates to itself.
  146. return ConstantEvalResult::NewSamePhase(inst);
  147. }
  148. auto EvalConstantInst(Context& context, SemIRLoc /*loc*/, SemIR::Converted inst)
  149. -> ConstantEvalResult {
  150. // A conversion evaluates to the result of the conversion.
  151. return ConstantEvalResult::Existing(
  152. context.constant_values().Get(inst.result_id));
  153. }
  154. auto EvalConstantInst(Context& /*context*/, SemIRLoc /*loc*/,
  155. SemIR::Deref /*inst*/) -> ConstantEvalResult {
  156. // TODO: Handle this.
  157. return ConstantEvalResult::TODO;
  158. }
  159. auto EvalConstantInst(Context& context, SemIRLoc /*loc*/,
  160. SemIR::ExportDecl inst) -> ConstantEvalResult {
  161. // An export instruction evaluates to the exported declaration.
  162. return ConstantEvalResult::Existing(
  163. context.constant_values().Get(inst.value_id));
  164. }
  165. auto EvalConstantInst(Context& context, SemIRLoc /*loc*/,
  166. SemIR::FacetAccessType inst) -> ConstantEvalResult {
  167. if (auto facet_value = context.insts().TryGetAs<SemIR::FacetValue>(
  168. inst.facet_value_inst_id)) {
  169. return ConstantEvalResult::Existing(
  170. context.constant_values().Get(facet_value->type_inst_id));
  171. }
  172. return ConstantEvalResult::NewSamePhase(inst);
  173. }
  174. auto EvalConstantInst(Context& context, SemIRLoc /*loc*/,
  175. SemIR::FacetAccessWitness inst) -> ConstantEvalResult {
  176. if (auto facet_value = context.insts().TryGetAs<SemIR::FacetValue>(
  177. inst.facet_value_inst_id)) {
  178. auto impl_witness_inst_id = context.inst_blocks().Get(
  179. facet_value->witnesses_block_id)[inst.index.index];
  180. return ConstantEvalResult::Existing(
  181. context.constant_values().Get(impl_witness_inst_id));
  182. }
  183. return ConstantEvalResult::NewSamePhase(inst);
  184. }
  185. auto EvalConstantInst(Context& context, SemIRLoc loc, SemIR::FloatType inst)
  186. -> ConstantEvalResult {
  187. return ValidateFloatType(context, loc, inst)
  188. ? ConstantEvalResult::NewSamePhase(inst)
  189. : ConstantEvalResult::Error;
  190. }
  191. auto EvalConstantInst(Context& /*context*/, SemIRLoc /*loc*/,
  192. SemIR::FunctionDecl inst) -> ConstantEvalResult {
  193. // A function declaration evaluates to a function object, which is an empty
  194. // object of function type.
  195. // TODO: Eventually we may need to handle captures here.
  196. return ConstantEvalResult::NewSamePhase(SemIR::StructValue{
  197. .type_id = inst.type_id, .elements_id = SemIR::InstBlockId::Empty});
  198. }
  199. auto EvalConstantInst(Context& context, SemIRLoc loc,
  200. SemIR::LookupImplWitness inst) -> ConstantEvalResult {
  201. auto result = EvalLookupSingleImplWitness(
  202. context, UnwrapSemIRLoc()(context, loc), inst);
  203. if (!result.has_value()) {
  204. // We use NotConstant to communicate back to impl lookup that the lookup
  205. // failed. This can not happen for a deferred symbolic lookup in a generic
  206. // eval block, since we only add the deferred lookup instruction (being
  207. // evaluated here) to the SemIR if the lookup succeeds.
  208. return ConstantEvalResult::NotConstant;
  209. }
  210. if (!result.has_concrete_value()) {
  211. return ConstantEvalResult::NewSamePhase(inst);
  212. }
  213. return ConstantEvalResult::Existing(
  214. context.constant_values().Get(result.concrete_witness()));
  215. }
  216. auto EvalConstantInst(Context& context, SemIRLoc loc,
  217. SemIR::ImplWitnessAccess inst) -> ConstantEvalResult {
  218. // This is PerformAggregateAccess followed by GetConstantInSpecific.
  219. if (auto witness =
  220. context.insts().TryGetAs<SemIR::ImplWitness>(inst.witness_id)) {
  221. auto elements = context.inst_blocks().Get(witness->elements_id);
  222. // `elements` can be empty if there is only a forward declaration of the
  223. // impl.
  224. if (!elements.empty()) {
  225. auto index = static_cast<size_t>(inst.index.index);
  226. CARBON_CHECK(index < elements.size(), "Access out of bounds.");
  227. auto element = elements[index];
  228. if (element.has_value()) {
  229. LoadImportRef(context, element);
  230. return ConstantEvalResult::Existing(GetConstantValueInSpecific(
  231. context.sem_ir(), witness->specific_id, element));
  232. }
  233. }
  234. CARBON_DIAGNOSTIC(
  235. ImplAccessMemberBeforeSet, Error,
  236. "accessing member from impl before it has a defined value");
  237. // TODO: Add note pointing to the impl declaration.
  238. context.emitter().Emit(loc, ImplAccessMemberBeforeSet);
  239. return ConstantEvalResult::Error;
  240. }
  241. return ConstantEvalResult::NewSamePhase(inst);
  242. }
  243. auto EvalConstantInst(Context& /*context*/, SemIRLoc /*loc*/,
  244. SemIR::ImportRefUnloaded inst) -> ConstantEvalResult {
  245. CARBON_FATAL("ImportRefUnloaded should be loaded before TryEvalInst: {0}",
  246. inst);
  247. }
  248. auto EvalConstantInst(Context& context, SemIRLoc /*loc*/,
  249. SemIR::InitializeFrom inst) -> ConstantEvalResult {
  250. // Initialization is not performed in-place during constant evaluation, so
  251. // just return the value of the initializer.
  252. return ConstantEvalResult::Existing(
  253. context.constant_values().Get(inst.src_id));
  254. }
  255. auto EvalConstantInst(Context& context, SemIRLoc loc, SemIR::IntType inst)
  256. -> ConstantEvalResult {
  257. return ValidateIntType(context, loc, inst)
  258. ? ConstantEvalResult::NewSamePhase(inst)
  259. : ConstantEvalResult::Error;
  260. }
  261. auto EvalConstantInst(Context& context, SemIRLoc /*loc*/,
  262. SemIR::InterfaceDecl inst) -> ConstantEvalResult {
  263. // If the interface has generic parameters, we don't produce an interface
  264. // type, but a callable whose return value is an interface type.
  265. if (context.interfaces().Get(inst.interface_id).has_parameters()) {
  266. return ConstantEvalResult::NewSamePhase(SemIR::StructValue{
  267. .type_id = inst.type_id, .elements_id = SemIR::InstBlockId::Empty});
  268. }
  269. // A non-generic interface declaration evaluates to a facet type.
  270. return ConstantEvalResult::NewSamePhase(FacetTypeFromInterface(
  271. context, inst.interface_id, SemIR::SpecificId::None));
  272. }
  273. auto EvalConstantInst(Context& context, SemIRLoc /*loc*/, SemIR::NameRef inst)
  274. -> ConstantEvalResult {
  275. // A name reference evaluates to the value the name resolves to.
  276. return ConstantEvalResult::Existing(
  277. context.constant_values().Get(inst.value_id));
  278. }
  279. auto EvalConstantInst(Context& context, SemIRLoc loc,
  280. SemIR::RequireCompleteType inst) -> ConstantEvalResult {
  281. auto witness_type_id =
  282. GetSingletonType(context, SemIR::WitnessType::SingletonInstId);
  283. // If the type is a concrete constant, require it to be complete now.
  284. auto complete_type_id = inst.complete_type_id;
  285. if (context.types().GetConstantId(complete_type_id).is_concrete()) {
  286. if (!TryToCompleteType(context, complete_type_id, loc, [&] {
  287. // TODO: It'd be nice to report the original type prior to
  288. // evaluation here.
  289. CARBON_DIAGNOSTIC(IncompleteTypeInMonomorphization, Error,
  290. "type {0} is incomplete", SemIR::TypeId);
  291. return context.emitter().Build(loc, IncompleteTypeInMonomorphization,
  292. complete_type_id);
  293. })) {
  294. return ConstantEvalResult::Error;
  295. }
  296. return ConstantEvalResult::NewSamePhase(SemIR::CompleteTypeWitness{
  297. .type_id = witness_type_id,
  298. .object_repr_id = context.types().GetObjectRepr(complete_type_id)});
  299. }
  300. // If it's not a concrete constant, require it to be complete once it
  301. // becomes one.
  302. return ConstantEvalResult::NewSamePhase(inst);
  303. }
  304. auto EvalConstantInst(Context& context, SemIRLoc /*loc*/,
  305. SemIR::SpecificConstant inst) -> ConstantEvalResult {
  306. // Pull the constant value out of the specific.
  307. return ConstantEvalResult::Existing(SemIR::GetConstantValueInSpecific(
  308. context.sem_ir(), inst.specific_id, inst.inst_id));
  309. }
  310. auto EvalConstantInst(Context& context, SemIRLoc loc,
  311. SemIR::SpecificImplFunction inst) -> ConstantEvalResult {
  312. auto callee_inst = context.insts().Get(inst.callee_id);
  313. // If the callee is not a function value, we're not ready to evaluate this
  314. // yet. Build a symbolic `SpecificImplFunction` constant.
  315. if (!callee_inst.Is<SemIR::StructValue>()) {
  316. return ConstantEvalResult::NewSamePhase(inst);
  317. }
  318. auto callee_type_id = callee_inst.type_id();
  319. auto callee_fn_type =
  320. context.types().TryGetAs<SemIR::FunctionType>(callee_type_id);
  321. if (!callee_fn_type) {
  322. return ConstantEvalResult::NewSamePhase(inst);
  323. }
  324. // If the callee function found in the impl witness is not generic, the result
  325. // is simply that function.
  326. // TODO: We could do this even before the callee is concrete.
  327. auto generic_id =
  328. context.functions().Get(callee_fn_type->function_id).generic_id;
  329. if (!generic_id.has_value()) {
  330. return ConstantEvalResult::Existing(
  331. context.constant_values().Get(inst.callee_id));
  332. }
  333. // Find the arguments to use.
  334. auto enclosing_specific_id = callee_fn_type->specific_id;
  335. auto enclosing_args = context.inst_blocks().Get(
  336. context.specifics().GetArgsOrEmpty(enclosing_specific_id));
  337. auto interface_fn_args = context.inst_blocks().Get(
  338. context.specifics().GetArgsOrEmpty(inst.specific_id));
  339. // Form new specific for the generic callee function. The arguments for this
  340. // specific are the enclosing arguments of the callee followed by the
  341. // remaining arguments from the interface function. Impl checking has ensured
  342. // that these arguments can also be used for the function in the impl witness.
  343. auto num_params = context.inst_blocks()
  344. .Get(context.generics().Get(generic_id).bindings_id)
  345. .size();
  346. llvm::SmallVector<SemIR::InstId> args;
  347. args.reserve(num_params);
  348. args.append(enclosing_args.begin(), enclosing_args.end());
  349. int remaining_params = num_params - args.size();
  350. CARBON_CHECK(static_cast<int>(interface_fn_args.size()) >= remaining_params);
  351. args.append(interface_fn_args.end() - remaining_params,
  352. interface_fn_args.end());
  353. auto specific_id = MakeSpecific(context, loc, generic_id, args);
  354. // TODO: Add the new `SpecificFunction` to definitions_required.
  355. return ConstantEvalResult::NewSamePhase(
  356. SemIR::SpecificFunction{.type_id = inst.type_id,
  357. .callee_id = inst.callee_id,
  358. .specific_id = specific_id});
  359. }
  360. auto EvalConstantInst(Context& context, SemIRLoc /*loc*/,
  361. SemIR::SpliceBlock inst) -> ConstantEvalResult {
  362. // SpliceBlock evaluates to the result value that is (typically) within the
  363. // block. This can be constant even if the block contains other non-constant
  364. // instructions.
  365. return ConstantEvalResult::Existing(
  366. context.constant_values().Get(inst.result_id));
  367. }
  368. auto EvalConstantInst(Context& context, SemIRLoc /*loc*/,
  369. SemIR::SpliceInst inst) -> ConstantEvalResult {
  370. // The constant value of a SpliceInst is the constant value of the instruction
  371. // being spliced. Note that `inst.inst_id` is the instruction being spliced,
  372. // so we need to go through another round of obtaining the constant value in
  373. // addition to the one performed by the eval infrastructure.
  374. if (auto inst_value =
  375. context.insts().TryGetAs<SemIR::InstValue>(inst.inst_id)) {
  376. return ConstantEvalResult::Existing(
  377. context.constant_values().Get(inst_value->inst_id));
  378. }
  379. // TODO: Consider creating a new `ValueOfInst` instruction analogous to
  380. // `TypeOfInst` to defer determining the constant value until we know the
  381. // instruction. Alternatively, produce a symbolic `SpliceInst` constant.
  382. return ConstantEvalResult::NotConstant;
  383. }
  384. auto EvalConstantInst(Context& context, SemIRLoc /*loc*/,
  385. SemIR::StructAccess inst) -> ConstantEvalResult {
  386. return PerformAggregateAccess(context, inst);
  387. }
  388. auto EvalConstantInst(Context& /*context*/, SemIRLoc /*loc*/,
  389. SemIR::StructInit inst) -> ConstantEvalResult {
  390. return ConstantEvalResult::NewSamePhase(SemIR::StructValue{
  391. .type_id = inst.type_id, .elements_id = inst.elements_id});
  392. }
  393. auto EvalConstantInst(Context& /*context*/, SemIRLoc /*loc*/,
  394. SemIR::Temporary /*inst*/) -> ConstantEvalResult {
  395. // TODO: Handle this. Can we just return the value of `init_id`?
  396. return ConstantEvalResult::TODO;
  397. }
  398. auto EvalConstantInst(Context& context, SemIRLoc /*loc*/,
  399. SemIR::TupleAccess inst) -> ConstantEvalResult {
  400. return PerformAggregateAccess(context, inst);
  401. }
  402. auto EvalConstantInst(Context& /*context*/, SemIRLoc /*loc*/,
  403. SemIR::TupleInit inst) -> ConstantEvalResult {
  404. return ConstantEvalResult::NewSamePhase(SemIR::TupleValue{
  405. .type_id = inst.type_id, .elements_id = inst.elements_id});
  406. }
  407. auto EvalConstantInst(Context& context, SemIRLoc /*loc*/,
  408. SemIR::TypeOfInst inst) -> ConstantEvalResult {
  409. // Grab the type from the instruction produced as our operand.
  410. if (auto inst_value =
  411. context.insts().TryGetAs<SemIR::InstValue>(inst.inst_id)) {
  412. return ConstantEvalResult::Existing(context.types().GetConstantId(
  413. context.insts().Get(inst_value->inst_id).type_id()));
  414. }
  415. return ConstantEvalResult::NewSamePhase(inst);
  416. }
  417. auto EvalConstantInst(Context& context, SemIRLoc /*loc*/,
  418. SemIR::UnaryOperatorNot inst) -> ConstantEvalResult {
  419. // `not true` -> `false`, `not false` -> `true`.
  420. // All other uses of unary `not` are non-constant.
  421. auto const_id = context.constant_values().Get(inst.operand_id);
  422. if (const_id.is_concrete()) {
  423. auto value = context.insts().GetAs<SemIR::BoolLiteral>(
  424. context.constant_values().GetInstId(const_id));
  425. value.value = SemIR::BoolValue::From(!value.value.ToBool());
  426. return ConstantEvalResult::NewSamePhase(value);
  427. }
  428. return ConstantEvalResult::NotConstant;
  429. }
  430. auto EvalConstantInst(Context& context, SemIRLoc /*loc*/,
  431. SemIR::ValueOfInitializer inst) -> ConstantEvalResult {
  432. // Values of value expressions and initializing expressions are represented in
  433. // the same way during constant evaluation, so just return the value of the
  434. // operand.
  435. return ConstantEvalResult::Existing(
  436. context.constant_values().Get(inst.init_id));
  437. }
  438. auto EvalConstantInst(Context& context, SemIRLoc /*loc*/,
  439. SemIR::ValueParamPattern inst) -> ConstantEvalResult {
  440. // TODO: Treat this as a non-expression (here and in GetExprCategory)
  441. // once generic deduction doesn't need patterns to have constant values.
  442. return ConstantEvalResult::Existing(
  443. context.constant_values().Get(inst.subpattern_id));
  444. }
  445. auto EvalConstantInst(Context& /*context*/, SemIRLoc /*loc*/,
  446. SemIR::VtablePtr /*inst*/) -> ConstantEvalResult {
  447. // TODO: Handle this.
  448. return ConstantEvalResult::TODO;
  449. }
  450. } // namespace Carbon::Check