eval_inst.cpp 21 KB

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