eval_inst.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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 "toolchain/check/facet_type.h"
  6. #include "toolchain/check/import_ref.h"
  7. #include "toolchain/check/type.h"
  8. #include "toolchain/check/type_completion.h"
  9. #include "toolchain/sem_ir/typed_insts.h"
  10. namespace Carbon::Check {
  11. // Performs an access into an aggregate, retrieving the specified element.
  12. static auto PerformAggregateAccess(Context& context, SemIR::Inst inst)
  13. -> ConstantEvalResult {
  14. auto access_inst = inst.As<SemIR::AnyAggregateAccess>();
  15. if (auto aggregate = context.insts().TryGetAs<SemIR::AnyAggregateValue>(
  16. access_inst.aggregate_id)) {
  17. auto elements = context.inst_blocks().Get(aggregate->elements_id);
  18. auto index = static_cast<size_t>(access_inst.index.index);
  19. CARBON_CHECK(index < elements.size(), "Access out of bounds.");
  20. // `Phase` is not used here. If this element is a concrete constant, then
  21. // so is the result of indexing, even if the aggregate also contains a
  22. // symbolic context.
  23. return ConstantEvalResult::Existing(
  24. context.constant_values().Get(elements[index]));
  25. }
  26. return ConstantEvalResult::NewSamePhase(inst);
  27. }
  28. auto EvalConstantInst(Context& /*context*/, SemIRLoc /*loc*/,
  29. SemIR::ArrayInit inst) -> ConstantEvalResult {
  30. // TODO: Add an `ArrayValue` to represent a constant array object
  31. // representation instead of using a `TupleValue`.
  32. return ConstantEvalResult::NewSamePhase(
  33. SemIR::TupleValue{.type_id = inst.type_id, .elements_id = inst.inits_id});
  34. }
  35. auto EvalConstantInst(Context& context, SemIRLoc loc, SemIR::ArrayType inst)
  36. -> ConstantEvalResult {
  37. auto bound_inst = context.insts().Get(inst.bound_id);
  38. auto int_bound = bound_inst.TryAs<SemIR::IntValue>();
  39. if (!int_bound) {
  40. CARBON_CHECK(context.constant_values().Get(inst.bound_id).is_symbolic(),
  41. "Unexpected inst {0} for template constant int", bound_inst);
  42. return ConstantEvalResult::NewSamePhase(inst);
  43. }
  44. // TODO: We should check that the size of the resulting array type
  45. // fits in 64 bits, not just that the bound does. Should we use a
  46. // 32-bit limit for 32-bit targets?
  47. const auto& bound_val = context.ints().Get(int_bound->int_id);
  48. if (context.types().IsSignedInt(int_bound->type_id) &&
  49. bound_val.isNegative()) {
  50. CARBON_DIAGNOSTIC(ArrayBoundNegative, Error,
  51. "array bound of {0} is negative", TypedInt);
  52. context.emitter().Emit(loc, ArrayBoundNegative,
  53. {.type = int_bound->type_id, .value = bound_val});
  54. return ConstantEvalResult::Error;
  55. }
  56. if (bound_val.getActiveBits() > 64) {
  57. CARBON_DIAGNOSTIC(ArrayBoundTooLarge, Error,
  58. "array bound of {0} is too large", TypedInt);
  59. context.emitter().Emit(loc, ArrayBoundTooLarge,
  60. {.type = int_bound->type_id, .value = bound_val});
  61. return ConstantEvalResult::Error;
  62. }
  63. return ConstantEvalResult::NewSamePhase(inst);
  64. }
  65. auto EvalConstantInst(Context& context, SemIRLoc /*loc*/,
  66. SemIR::AsCompatible inst) -> ConstantEvalResult {
  67. // AsCompatible changes the type of the source instruction; its constant
  68. // value, if there is one, needs to be modified to be of the same type.
  69. auto value_id = context.constant_values().Get(inst.source_id);
  70. CARBON_CHECK(value_id.is_constant());
  71. auto value_inst =
  72. context.insts().Get(context.constant_values().GetInstId(value_id));
  73. value_inst.SetType(inst.type_id);
  74. return ConstantEvalResult::NewAnyPhase(value_inst);
  75. }
  76. auto EvalConstantInst(Context& context, SemIRLoc /*loc*/, SemIR::BindAlias inst)
  77. -> ConstantEvalResult {
  78. // An alias evaluates to the value it's bound to.
  79. return ConstantEvalResult::Existing(
  80. context.constant_values().Get(inst.value_id));
  81. }
  82. auto EvalConstantInst(Context& /*context*/, SemIRLoc /*loc*/,
  83. SemIR::BindValue /*inst*/) -> ConstantEvalResult {
  84. // TODO: Handle this once we've decided how to represent constant values of
  85. // reference expressions.
  86. return ConstantEvalResult::TODO;
  87. }
  88. auto EvalConstantInst(Context& context, SemIRLoc /*loc*/,
  89. SemIR::ClassElementAccess inst) -> ConstantEvalResult {
  90. return PerformAggregateAccess(context, inst);
  91. }
  92. auto EvalConstantInst(Context& context, SemIRLoc /*loc*/, SemIR::ClassDecl inst)
  93. -> ConstantEvalResult {
  94. // If the class has generic parameters, we don't produce a class type, but a
  95. // callable whose return value is a class type.
  96. if (context.classes().Get(inst.class_id).has_parameters()) {
  97. return ConstantEvalResult::NewSamePhase(SemIR::StructValue{
  98. .type_id = inst.type_id, .elements_id = SemIR::InstBlockId::Empty});
  99. }
  100. // A non-generic class declaration evaluates to the class type.
  101. return ConstantEvalResult::NewSamePhase(
  102. SemIR::ClassType{.type_id = SemIR::TypeType::SingletonTypeId,
  103. .class_id = inst.class_id,
  104. .specific_id = SemIR::SpecificId::None});
  105. }
  106. auto EvalConstantInst(Context& /*context*/, SemIRLoc /*loc*/,
  107. SemIR::ClassInit inst) -> ConstantEvalResult {
  108. // TODO: Add a `ClassValue` to represent a constant class object
  109. // representation instead of using a `StructValue`.
  110. return ConstantEvalResult::NewSamePhase(SemIR::StructValue{
  111. .type_id = inst.type_id, .elements_id = inst.elements_id});
  112. }
  113. auto EvalConstantInst(Context& context, SemIRLoc /*loc*/, SemIR::ConstType inst)
  114. -> ConstantEvalResult {
  115. // `const (const T)` evaluates to `const T`.
  116. if (context.types().Is<SemIR::ConstType>(inst.inner_id)) {
  117. return ConstantEvalResult::Existing(
  118. context.types().GetConstantId(inst.inner_id));
  119. }
  120. // Otherwise, `const T` evaluates to itself.
  121. return ConstantEvalResult::NewSamePhase(inst);
  122. }
  123. auto EvalConstantInst(Context& context, SemIRLoc /*loc*/, SemIR::Converted inst)
  124. -> ConstantEvalResult {
  125. // A conversion evaluates to the result of the conversion.
  126. return ConstantEvalResult::Existing(
  127. context.constant_values().Get(inst.result_id));
  128. }
  129. auto EvalConstantInst(Context& /*context*/, SemIRLoc /*loc*/,
  130. SemIR::Deref /*inst*/) -> ConstantEvalResult {
  131. // TODO: Handle this.
  132. return ConstantEvalResult::TODO;
  133. }
  134. auto EvalConstantInst(Context& context, SemIRLoc /*loc*/,
  135. SemIR::ExportDecl inst) -> ConstantEvalResult {
  136. // An export instruction evaluates to the exported declaration.
  137. return ConstantEvalResult::Existing(
  138. context.constant_values().Get(inst.value_id));
  139. }
  140. auto EvalConstantInst(Context& context, SemIRLoc /*loc*/,
  141. SemIR::FacetAccessType inst) -> ConstantEvalResult {
  142. if (auto facet_value = context.insts().TryGetAs<SemIR::FacetValue>(
  143. inst.facet_value_inst_id)) {
  144. return ConstantEvalResult::Existing(
  145. context.constant_values().Get(facet_value->type_inst_id));
  146. }
  147. return ConstantEvalResult::NewSamePhase(inst);
  148. }
  149. auto EvalConstantInst(Context& context, SemIRLoc /*loc*/,
  150. SemIR::FacetAccessWitness inst) -> ConstantEvalResult {
  151. if (auto facet_value = context.insts().TryGetAs<SemIR::FacetValue>(
  152. inst.facet_value_inst_id)) {
  153. auto impl_witness_inst_id = context.inst_blocks().Get(
  154. facet_value->witnesses_block_id)[inst.index.index];
  155. return ConstantEvalResult::Existing(
  156. context.constant_values().Get(impl_witness_inst_id));
  157. }
  158. return ConstantEvalResult::NewSamePhase(inst);
  159. }
  160. auto EvalConstantInst(Context& context, SemIRLoc loc, SemIR::FloatType inst)
  161. -> ConstantEvalResult {
  162. return ValidateFloatType(context, loc, inst)
  163. ? ConstantEvalResult::NewSamePhase(inst)
  164. : ConstantEvalResult::Error;
  165. }
  166. auto EvalConstantInst(Context& /*context*/, SemIRLoc /*loc*/,
  167. SemIR::FunctionDecl inst) -> ConstantEvalResult {
  168. // A function declaration evaluates to a function object, which is an empty
  169. // object of function type.
  170. // TODO: Eventually we may need to handle captures here.
  171. return ConstantEvalResult::NewSamePhase(SemIR::StructValue{
  172. .type_id = inst.type_id, .elements_id = SemIR::InstBlockId::Empty});
  173. }
  174. auto EvalConstantInst(Context& context, SemIRLoc loc,
  175. SemIR::ImplWitnessAccess inst) -> ConstantEvalResult {
  176. // This is PerformAggregateAccess followed by GetConstantInSpecific.
  177. if (auto witness =
  178. context.insts().TryGetAs<SemIR::ImplWitness>(inst.witness_id)) {
  179. auto elements = context.inst_blocks().Get(witness->elements_id);
  180. auto index = static_cast<size_t>(inst.index.index);
  181. CARBON_CHECK(index < elements.size(), "Access out of bounds.");
  182. auto element = elements[index];
  183. if (!element.has_value()) {
  184. // TODO: Perhaps this should be a `{}` value with incomplete type?
  185. CARBON_DIAGNOSTIC(ImplAccessMemberBeforeComplete, Error,
  186. "accessing member from impl before the end of "
  187. "its definition");
  188. // TODO: Add note pointing to the impl declaration.
  189. context.emitter().Emit(loc, ImplAccessMemberBeforeComplete);
  190. return ConstantEvalResult::Error;
  191. }
  192. LoadImportRef(context, element);
  193. return ConstantEvalResult::Existing(GetConstantValueInSpecific(
  194. context.sem_ir(), witness->specific_id, element));
  195. }
  196. return ConstantEvalResult::NewSamePhase(inst);
  197. }
  198. auto EvalConstantInst(Context& /*context*/, SemIRLoc /*loc*/,
  199. SemIR::ImportRefUnloaded inst) -> ConstantEvalResult {
  200. CARBON_FATAL("ImportRefUnloaded should be loaded before TryEvalInst: {0}",
  201. inst);
  202. }
  203. auto EvalConstantInst(Context& context, SemIRLoc /*loc*/,
  204. SemIR::InitializeFrom inst) -> ConstantEvalResult {
  205. // Initialization is not performed in-place during constant evaluation, so
  206. // just return the value of the initializer.
  207. return ConstantEvalResult::Existing(
  208. context.constant_values().Get(inst.src_id));
  209. }
  210. auto EvalConstantInst(Context& context, SemIRLoc loc, SemIR::IntType inst)
  211. -> ConstantEvalResult {
  212. return ValidateIntType(context, loc, inst)
  213. ? ConstantEvalResult::NewSamePhase(inst)
  214. : ConstantEvalResult::Error;
  215. }
  216. auto EvalConstantInst(Context& context, SemIRLoc /*loc*/,
  217. SemIR::InterfaceDecl inst) -> ConstantEvalResult {
  218. // If the interface has generic parameters, we don't produce an interface
  219. // type, but a callable whose return value is an interface type.
  220. if (context.interfaces().Get(inst.interface_id).has_parameters()) {
  221. return ConstantEvalResult::NewSamePhase(SemIR::StructValue{
  222. .type_id = inst.type_id, .elements_id = SemIR::InstBlockId::Empty});
  223. }
  224. // A non-generic interface declaration evaluates to a facet type.
  225. return ConstantEvalResult::NewSamePhase(FacetTypeFromInterface(
  226. context, inst.interface_id, SemIR::SpecificId::None));
  227. }
  228. auto EvalConstantInst(Context& context, SemIRLoc /*loc*/, SemIR::NameRef inst)
  229. -> ConstantEvalResult {
  230. // A name reference evaluates to the value the name resolves to.
  231. return ConstantEvalResult::Existing(
  232. context.constant_values().Get(inst.value_id));
  233. }
  234. auto EvalConstantInst(Context& context, SemIRLoc loc,
  235. SemIR::RequireCompleteType inst) -> ConstantEvalResult {
  236. auto witness_type_id =
  237. GetSingletonType(context, SemIR::WitnessType::SingletonInstId);
  238. // If the type is a concrete constant, require it to be complete now.
  239. auto complete_type_id = inst.complete_type_id;
  240. if (context.types().GetConstantId(complete_type_id).is_concrete()) {
  241. if (!TryToCompleteType(context, complete_type_id, loc, [&] {
  242. // TODO: It'd be nice to report the original type prior to
  243. // evaluation here.
  244. CARBON_DIAGNOSTIC(IncompleteTypeInMonomorphization, Error,
  245. "type {0} is incomplete", SemIR::TypeId);
  246. return context.emitter().Build(loc, IncompleteTypeInMonomorphization,
  247. complete_type_id);
  248. })) {
  249. return ConstantEvalResult::Error;
  250. }
  251. return ConstantEvalResult::NewSamePhase(SemIR::CompleteTypeWitness{
  252. .type_id = witness_type_id,
  253. .object_repr_id = context.types().GetObjectRepr(complete_type_id)});
  254. }
  255. // If it's not a concrete constant, require it to be complete once it
  256. // becomes one.
  257. return ConstantEvalResult::NewSamePhase(inst);
  258. }
  259. auto EvalConstantInst(Context& context, SemIRLoc /*loc*/,
  260. SemIR::SpecificConstant inst) -> ConstantEvalResult {
  261. // Pull the constant value out of the specific.
  262. return ConstantEvalResult::Existing(SemIR::GetConstantValueInSpecific(
  263. context.sem_ir(), inst.specific_id, inst.inst_id));
  264. }
  265. auto EvalConstantInst(Context& context, SemIRLoc /*loc*/,
  266. SemIR::SpliceBlock inst) -> ConstantEvalResult {
  267. // SpliceBlock evaluates to the result value that is (typically) within the
  268. // block. This can be constant even if the block contains other non-constant
  269. // instructions.
  270. return ConstantEvalResult::Existing(
  271. context.constant_values().Get(inst.result_id));
  272. }
  273. auto EvalConstantInst(Context& context, SemIRLoc /*loc*/,
  274. SemIR::StructAccess inst) -> ConstantEvalResult {
  275. return PerformAggregateAccess(context, inst);
  276. }
  277. auto EvalConstantInst(Context& /*context*/, SemIRLoc /*loc*/,
  278. SemIR::StructInit inst) -> ConstantEvalResult {
  279. return ConstantEvalResult::NewSamePhase(SemIR::StructValue{
  280. .type_id = inst.type_id, .elements_id = inst.elements_id});
  281. }
  282. auto EvalConstantInst(Context& /*context*/, SemIRLoc /*loc*/,
  283. SemIR::Temporary /*inst*/) -> ConstantEvalResult {
  284. // TODO: Handle this. Can we just return the value of `init_id`?
  285. return ConstantEvalResult::TODO;
  286. }
  287. auto EvalConstantInst(Context& context, SemIRLoc /*loc*/,
  288. SemIR::TupleAccess inst) -> ConstantEvalResult {
  289. return PerformAggregateAccess(context, inst);
  290. }
  291. auto EvalConstantInst(Context& /*context*/, SemIRLoc /*loc*/,
  292. SemIR::TupleInit inst) -> ConstantEvalResult {
  293. return ConstantEvalResult::NewSamePhase(SemIR::TupleValue{
  294. .type_id = inst.type_id, .elements_id = inst.elements_id});
  295. }
  296. auto EvalConstantInst(Context& context, SemIRLoc /*loc*/,
  297. SemIR::UnaryOperatorNot inst) -> ConstantEvalResult {
  298. // `not true` -> `false`, `not false` -> `true`.
  299. // All other uses of unary `not` are non-constant.
  300. auto const_id = context.constant_values().Get(inst.operand_id);
  301. if (const_id.is_concrete()) {
  302. auto value = context.insts().GetAs<SemIR::BoolLiteral>(
  303. context.constant_values().GetInstId(const_id));
  304. value.value = SemIR::BoolValue::From(!value.value.ToBool());
  305. return ConstantEvalResult::NewSamePhase(value);
  306. }
  307. return ConstantEvalResult::NotConstant;
  308. }
  309. auto EvalConstantInst(Context& context, SemIRLoc /*loc*/,
  310. SemIR::ValueOfInitializer inst) -> ConstantEvalResult {
  311. // Values of value expressions and initializing expressions are represented in
  312. // the same way during constant evaluation, so just return the value of the
  313. // operand.
  314. return ConstantEvalResult::Existing(
  315. context.constant_values().Get(inst.init_id));
  316. }
  317. auto EvalConstantInst(Context& context, SemIRLoc /*loc*/,
  318. SemIR::ValueParamPattern inst) -> ConstantEvalResult {
  319. // TODO: Treat this as a non-expression (here and in GetExprCategory)
  320. // once generic deduction doesn't need patterns to have constant values.
  321. return ConstantEvalResult::Existing(
  322. context.constant_values().Get(inst.subpattern_id));
  323. }
  324. auto EvalConstantInst(Context& /*context*/, SemIRLoc /*loc*/,
  325. SemIR::VtablePtr /*inst*/) -> ConstantEvalResult {
  326. // TODO: Handle this.
  327. return ConstantEvalResult::TODO;
  328. }
  329. } // namespace Carbon::Check