eval_inst.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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::ImplSymbolicWitness 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. auto index = static_cast<size_t>(inst.index.index);
  223. CARBON_CHECK(index < elements.size(), "Access out of bounds.");
  224. auto element = elements[index];
  225. if (!element.has_value()) {
  226. // TODO: Perhaps this should be a `{}` value with incomplete type?
  227. CARBON_DIAGNOSTIC(ImplAccessMemberBeforeComplete, Error,
  228. "accessing member from impl before the end of "
  229. "its definition");
  230. // TODO: Add note pointing to the impl declaration.
  231. context.emitter().Emit(loc, ImplAccessMemberBeforeComplete);
  232. return ConstantEvalResult::Error;
  233. }
  234. LoadImportRef(context, element);
  235. return ConstantEvalResult::Existing(GetConstantValueInSpecific(
  236. context.sem_ir(), witness->specific_id, element));
  237. }
  238. return ConstantEvalResult::NewSamePhase(inst);
  239. }
  240. auto EvalConstantInst(Context& /*context*/, SemIRLoc /*loc*/,
  241. SemIR::ImportRefUnloaded inst) -> ConstantEvalResult {
  242. CARBON_FATAL("ImportRefUnloaded should be loaded before TryEvalInst: {0}",
  243. inst);
  244. }
  245. auto EvalConstantInst(Context& context, SemIRLoc /*loc*/,
  246. SemIR::InitializeFrom inst) -> ConstantEvalResult {
  247. // Initialization is not performed in-place during constant evaluation, so
  248. // just return the value of the initializer.
  249. return ConstantEvalResult::Existing(
  250. context.constant_values().Get(inst.src_id));
  251. }
  252. auto EvalConstantInst(Context& context, SemIRLoc loc, SemIR::IntType inst)
  253. -> ConstantEvalResult {
  254. return ValidateIntType(context, loc, inst)
  255. ? ConstantEvalResult::NewSamePhase(inst)
  256. : ConstantEvalResult::Error;
  257. }
  258. auto EvalConstantInst(Context& context, SemIRLoc /*loc*/,
  259. SemIR::InterfaceDecl inst) -> ConstantEvalResult {
  260. // If the interface has generic parameters, we don't produce an interface
  261. // type, but a callable whose return value is an interface type.
  262. if (context.interfaces().Get(inst.interface_id).has_parameters()) {
  263. return ConstantEvalResult::NewSamePhase(SemIR::StructValue{
  264. .type_id = inst.type_id, .elements_id = SemIR::InstBlockId::Empty});
  265. }
  266. // A non-generic interface declaration evaluates to a facet type.
  267. return ConstantEvalResult::NewSamePhase(FacetTypeFromInterface(
  268. context, inst.interface_id, SemIR::SpecificId::None));
  269. }
  270. auto EvalConstantInst(Context& context, SemIRLoc /*loc*/, SemIR::NameRef inst)
  271. -> ConstantEvalResult {
  272. // A name reference evaluates to the value the name resolves to.
  273. return ConstantEvalResult::Existing(
  274. context.constant_values().Get(inst.value_id));
  275. }
  276. auto EvalConstantInst(Context& context, SemIRLoc loc,
  277. SemIR::RequireCompleteType inst) -> ConstantEvalResult {
  278. auto witness_type_id =
  279. GetSingletonType(context, SemIR::WitnessType::SingletonInstId);
  280. // If the type is a concrete constant, require it to be complete now.
  281. auto complete_type_id = inst.complete_type_id;
  282. if (context.types().GetConstantId(complete_type_id).is_concrete()) {
  283. if (!TryToCompleteType(context, complete_type_id, loc, [&] {
  284. // TODO: It'd be nice to report the original type prior to
  285. // evaluation here.
  286. CARBON_DIAGNOSTIC(IncompleteTypeInMonomorphization, Error,
  287. "type {0} is incomplete", SemIR::TypeId);
  288. return context.emitter().Build(loc, IncompleteTypeInMonomorphization,
  289. complete_type_id);
  290. })) {
  291. return ConstantEvalResult::Error;
  292. }
  293. return ConstantEvalResult::NewSamePhase(SemIR::CompleteTypeWitness{
  294. .type_id = witness_type_id,
  295. .object_repr_id = context.types().GetObjectRepr(complete_type_id)});
  296. }
  297. // If it's not a concrete constant, require it to be complete once it
  298. // becomes one.
  299. return ConstantEvalResult::NewSamePhase(inst);
  300. }
  301. auto EvalConstantInst(Context& context, SemIRLoc /*loc*/,
  302. SemIR::SpecificConstant inst) -> ConstantEvalResult {
  303. // Pull the constant value out of the specific.
  304. return ConstantEvalResult::Existing(SemIR::GetConstantValueInSpecific(
  305. context.sem_ir(), inst.specific_id, inst.inst_id));
  306. }
  307. auto EvalConstantInst(Context& context, SemIRLoc loc,
  308. SemIR::SpecificImplFunction inst) -> ConstantEvalResult {
  309. auto callee_inst = context.insts().Get(inst.callee_id);
  310. // If the callee is not a function value, we're not ready to evaluate this
  311. // yet. Build a symbolic `SpecificImplFunction` constant.
  312. if (!callee_inst.Is<SemIR::StructValue>()) {
  313. return ConstantEvalResult::NewSamePhase(inst);
  314. }
  315. auto callee_type_id = callee_inst.type_id();
  316. auto callee_fn_type =
  317. context.types().TryGetAs<SemIR::FunctionType>(callee_type_id);
  318. if (!callee_fn_type) {
  319. return ConstantEvalResult::NewSamePhase(inst);
  320. }
  321. // If the callee function found in the impl witness is not generic, the result
  322. // is simply that function.
  323. // TODO: We could do this even before the callee is concrete.
  324. auto generic_id =
  325. context.functions().Get(callee_fn_type->function_id).generic_id;
  326. if (!generic_id.has_value()) {
  327. return ConstantEvalResult::Existing(
  328. context.constant_values().Get(inst.callee_id));
  329. }
  330. // Find the arguments to use.
  331. auto enclosing_specific_id = callee_fn_type->specific_id;
  332. auto enclosing_args = context.inst_blocks().Get(
  333. context.specifics().GetArgsOrEmpty(enclosing_specific_id));
  334. auto interface_fn_args = context.inst_blocks().Get(
  335. context.specifics().GetArgsOrEmpty(inst.specific_id));
  336. // Form new specific for the generic callee function. The arguments for this
  337. // specific are the enclosing arguments of the callee followed by the
  338. // remaining arguments from the interface function. Impl checking has ensured
  339. // that these arguments can also be used for the function in the impl witness.
  340. auto num_params = context.inst_blocks()
  341. .Get(context.generics().Get(generic_id).bindings_id)
  342. .size();
  343. llvm::SmallVector<SemIR::InstId> args;
  344. args.reserve(num_params);
  345. args.append(enclosing_args.begin(), enclosing_args.end());
  346. int remaining_params = num_params - args.size();
  347. CARBON_CHECK(static_cast<int>(interface_fn_args.size()) >= remaining_params);
  348. args.append(interface_fn_args.end() - remaining_params,
  349. interface_fn_args.end());
  350. auto specific_id = MakeSpecific(context, loc, generic_id, args);
  351. // TODO: Add the new `SpecificFunction` to definitions_required.
  352. return ConstantEvalResult::NewSamePhase(
  353. SemIR::SpecificFunction{.type_id = inst.type_id,
  354. .callee_id = inst.callee_id,
  355. .specific_id = specific_id});
  356. }
  357. auto EvalConstantInst(Context& context, SemIRLoc /*loc*/,
  358. SemIR::SpliceBlock inst) -> ConstantEvalResult {
  359. // SpliceBlock evaluates to the result value that is (typically) within the
  360. // block. This can be constant even if the block contains other non-constant
  361. // instructions.
  362. return ConstantEvalResult::Existing(
  363. context.constant_values().Get(inst.result_id));
  364. }
  365. auto EvalConstantInst(Context& context, SemIRLoc /*loc*/,
  366. SemIR::SpliceInst inst) -> ConstantEvalResult {
  367. // The constant value of a SpliceInst is the constant value of the instruction
  368. // being spliced. Note that `inst.inst_id` is the instruction being spliced,
  369. // so we need to go through another round of obtaining the constant value in
  370. // addition to the one performed by the eval infrastructure.
  371. if (auto inst_value =
  372. context.insts().TryGetAs<SemIR::InstValue>(inst.inst_id)) {
  373. return ConstantEvalResult::Existing(
  374. context.constant_values().Get(inst_value->inst_id));
  375. }
  376. // TODO: Consider creating a new `ValueOfInst` instruction analogous to
  377. // `TypeOfInst` to defer determining the constant value until we know the
  378. // instruction. Alternatively, produce a symbolic `SpliceInst` constant.
  379. return ConstantEvalResult::NotConstant;
  380. }
  381. auto EvalConstantInst(Context& context, SemIRLoc /*loc*/,
  382. SemIR::StructAccess inst) -> ConstantEvalResult {
  383. return PerformAggregateAccess(context, inst);
  384. }
  385. auto EvalConstantInst(Context& /*context*/, SemIRLoc /*loc*/,
  386. SemIR::StructInit inst) -> ConstantEvalResult {
  387. return ConstantEvalResult::NewSamePhase(SemIR::StructValue{
  388. .type_id = inst.type_id, .elements_id = inst.elements_id});
  389. }
  390. auto EvalConstantInst(Context& /*context*/, SemIRLoc /*loc*/,
  391. SemIR::Temporary /*inst*/) -> ConstantEvalResult {
  392. // TODO: Handle this. Can we just return the value of `init_id`?
  393. return ConstantEvalResult::TODO;
  394. }
  395. auto EvalConstantInst(Context& context, SemIRLoc /*loc*/,
  396. SemIR::TupleAccess inst) -> ConstantEvalResult {
  397. return PerformAggregateAccess(context, inst);
  398. }
  399. auto EvalConstantInst(Context& /*context*/, SemIRLoc /*loc*/,
  400. SemIR::TupleInit inst) -> ConstantEvalResult {
  401. return ConstantEvalResult::NewSamePhase(SemIR::TupleValue{
  402. .type_id = inst.type_id, .elements_id = inst.elements_id});
  403. }
  404. auto EvalConstantInst(Context& context, SemIRLoc /*loc*/,
  405. SemIR::TypeOfInst inst) -> ConstantEvalResult {
  406. // Grab the type from the instruction produced as our operand.
  407. if (auto inst_value =
  408. context.insts().TryGetAs<SemIR::InstValue>(inst.inst_id)) {
  409. return ConstantEvalResult::Existing(context.types().GetConstantId(
  410. context.insts().Get(inst_value->inst_id).type_id()));
  411. }
  412. return ConstantEvalResult::NewSamePhase(inst);
  413. }
  414. auto EvalConstantInst(Context& context, SemIRLoc /*loc*/,
  415. SemIR::UnaryOperatorNot inst) -> ConstantEvalResult {
  416. // `not true` -> `false`, `not false` -> `true`.
  417. // All other uses of unary `not` are non-constant.
  418. auto const_id = context.constant_values().Get(inst.operand_id);
  419. if (const_id.is_concrete()) {
  420. auto value = context.insts().GetAs<SemIR::BoolLiteral>(
  421. context.constant_values().GetInstId(const_id));
  422. value.value = SemIR::BoolValue::From(!value.value.ToBool());
  423. return ConstantEvalResult::NewSamePhase(value);
  424. }
  425. return ConstantEvalResult::NotConstant;
  426. }
  427. auto EvalConstantInst(Context& context, SemIRLoc /*loc*/,
  428. SemIR::ValueOfInitializer inst) -> ConstantEvalResult {
  429. // Values of value expressions and initializing expressions are represented in
  430. // the same way during constant evaluation, so just return the value of the
  431. // operand.
  432. return ConstantEvalResult::Existing(
  433. context.constant_values().Get(inst.init_id));
  434. }
  435. auto EvalConstantInst(Context& context, SemIRLoc /*loc*/,
  436. SemIR::ValueParamPattern inst) -> ConstantEvalResult {
  437. // TODO: Treat this as a non-expression (here and in GetExprCategory)
  438. // once generic deduction doesn't need patterns to have constant values.
  439. return ConstantEvalResult::Existing(
  440. context.constant_values().Get(inst.subpattern_id));
  441. }
  442. auto EvalConstantInst(Context& /*context*/, SemIRLoc /*loc*/,
  443. SemIR::VtablePtr /*inst*/) -> ConstantEvalResult {
  444. // TODO: Handle this.
  445. return ConstantEvalResult::TODO;
  446. }
  447. } // namespace Carbon::Check