eval_inst.cpp 22 KB

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