eval_inst.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. return ConstantEvalResult::Existing(
  154. context.constant_values().Get(facet_value->witness_inst_id));
  155. }
  156. return ConstantEvalResult::NewSamePhase(inst);
  157. }
  158. auto EvalConstantInst(Context& context, SemIRLoc loc, SemIR::FloatType inst)
  159. -> ConstantEvalResult {
  160. return ValidateFloatType(context, loc, inst)
  161. ? ConstantEvalResult::NewSamePhase(inst)
  162. : ConstantEvalResult::Error;
  163. }
  164. auto EvalConstantInst(Context& /*context*/, SemIRLoc /*loc*/,
  165. SemIR::FunctionDecl inst) -> ConstantEvalResult {
  166. // A function declaration evaluates to a function object, which is an empty
  167. // object of function type.
  168. // TODO: Eventually we may need to handle captures here.
  169. return ConstantEvalResult::NewSamePhase(SemIR::StructValue{
  170. .type_id = inst.type_id, .elements_id = SemIR::InstBlockId::Empty});
  171. }
  172. auto EvalConstantInst(Context& context, SemIRLoc loc,
  173. SemIR::ImplWitnessAccess inst) -> ConstantEvalResult {
  174. // This is PerformAggregateAccess followed by GetConstantInSpecific.
  175. if (auto witness =
  176. context.insts().TryGetAs<SemIR::ImplWitness>(inst.witness_id)) {
  177. auto elements = context.inst_blocks().Get(witness->elements_id);
  178. auto index = static_cast<size_t>(inst.index.index);
  179. // TODO: Remove this block when LookupImplWitness returns all the witnesses
  180. // for a facet type instead of just one. We just don't want to introduce
  181. // crashes in the meantime.
  182. if (index >= elements.size()) {
  183. context.TODO(loc,
  184. "incorrect witness for multiple interfaces in a facet type");
  185. return ConstantEvalResult::Error;
  186. }
  187. CARBON_CHECK(index < elements.size(), "Access out of bounds.");
  188. auto element = elements[index];
  189. if (!element.has_value()) {
  190. // TODO: Perhaps this should be a `{}` value with incomplete type?
  191. CARBON_DIAGNOSTIC(ImplAccessMemberBeforeComplete, Error,
  192. "accessing member from impl before the end of "
  193. "its definition");
  194. // TODO: Add note pointing to the impl declaration.
  195. context.emitter().Emit(loc, ImplAccessMemberBeforeComplete);
  196. return ConstantEvalResult::Error;
  197. }
  198. LoadImportRef(context, element);
  199. return ConstantEvalResult::Existing(GetConstantValueInSpecific(
  200. context.sem_ir(), witness->specific_id, element));
  201. }
  202. return ConstantEvalResult::NewSamePhase(inst);
  203. }
  204. auto EvalConstantInst(Context& /*context*/, SemIRLoc /*loc*/,
  205. SemIR::ImportRefUnloaded inst) -> ConstantEvalResult {
  206. CARBON_FATAL("ImportRefUnloaded should be loaded before TryEvalInst: {0}",
  207. inst);
  208. }
  209. auto EvalConstantInst(Context& context, SemIRLoc /*loc*/,
  210. SemIR::InitializeFrom inst) -> ConstantEvalResult {
  211. // Initialization is not performed in-place during constant evaluation, so
  212. // just return the value of the initializer.
  213. return ConstantEvalResult::Existing(
  214. context.constant_values().Get(inst.src_id));
  215. }
  216. auto EvalConstantInst(Context& context, SemIRLoc loc, SemIR::IntType inst)
  217. -> ConstantEvalResult {
  218. return ValidateIntType(context, loc, inst)
  219. ? ConstantEvalResult::NewSamePhase(inst)
  220. : ConstantEvalResult::Error;
  221. }
  222. auto EvalConstantInst(Context& context, SemIRLoc /*loc*/,
  223. SemIR::InterfaceDecl inst) -> ConstantEvalResult {
  224. // If the interface has generic parameters, we don't produce an interface
  225. // type, but a callable whose return value is an interface type.
  226. if (context.interfaces().Get(inst.interface_id).has_parameters()) {
  227. return ConstantEvalResult::NewSamePhase(SemIR::StructValue{
  228. .type_id = inst.type_id, .elements_id = SemIR::InstBlockId::Empty});
  229. }
  230. // A non-generic interface declaration evaluates to a facet type.
  231. return ConstantEvalResult::NewSamePhase(FacetTypeFromInterface(
  232. context, inst.interface_id, SemIR::SpecificId::None));
  233. }
  234. auto EvalConstantInst(Context& context, SemIRLoc /*loc*/, SemIR::NameRef inst)
  235. -> ConstantEvalResult {
  236. // A name reference evaluates to the value the name resolves to.
  237. return ConstantEvalResult::Existing(
  238. context.constant_values().Get(inst.value_id));
  239. }
  240. auto EvalConstantInst(Context& context, SemIRLoc loc,
  241. SemIR::RequireCompleteType inst) -> ConstantEvalResult {
  242. auto witness_type_id =
  243. GetSingletonType(context, SemIR::WitnessType::SingletonInstId);
  244. // If the type is a concrete constant, require it to be complete now.
  245. auto complete_type_id = inst.complete_type_id;
  246. if (context.types().GetConstantId(complete_type_id).is_concrete()) {
  247. if (!TryToCompleteType(context, complete_type_id, loc, [&] {
  248. // TODO: It'd be nice to report the original type prior to
  249. // evaluation here.
  250. CARBON_DIAGNOSTIC(IncompleteTypeInMonomorphization, Error,
  251. "type {0} is incomplete", SemIR::TypeId);
  252. return context.emitter().Build(loc, IncompleteTypeInMonomorphization,
  253. complete_type_id);
  254. })) {
  255. return ConstantEvalResult::Error;
  256. }
  257. return ConstantEvalResult::NewSamePhase(SemIR::CompleteTypeWitness{
  258. .type_id = witness_type_id,
  259. .object_repr_id = context.types().GetObjectRepr(complete_type_id)});
  260. }
  261. // If it's not a concrete constant, require it to be complete once it
  262. // becomes one.
  263. return ConstantEvalResult::NewSamePhase(inst);
  264. }
  265. auto EvalConstantInst(Context& context, SemIRLoc /*loc*/,
  266. SemIR::SpecificConstant inst) -> ConstantEvalResult {
  267. // Pull the constant value out of the specific.
  268. return ConstantEvalResult::Existing(SemIR::GetConstantValueInSpecific(
  269. context.sem_ir(), inst.specific_id, inst.inst_id));
  270. }
  271. auto EvalConstantInst(Context& context, SemIRLoc /*loc*/,
  272. SemIR::SpliceBlock inst) -> ConstantEvalResult {
  273. // SpliceBlock evaluates to the result value that is (typically) within the
  274. // block. This can be constant even if the block contains other non-constant
  275. // instructions.
  276. return ConstantEvalResult::Existing(
  277. context.constant_values().Get(inst.result_id));
  278. }
  279. auto EvalConstantInst(Context& context, SemIRLoc /*loc*/,
  280. SemIR::StructAccess inst) -> ConstantEvalResult {
  281. return PerformAggregateAccess(context, inst);
  282. }
  283. auto EvalConstantInst(Context& /*context*/, SemIRLoc /*loc*/,
  284. SemIR::StructInit inst) -> ConstantEvalResult {
  285. return ConstantEvalResult::NewSamePhase(SemIR::StructValue{
  286. .type_id = inst.type_id, .elements_id = inst.elements_id});
  287. }
  288. auto EvalConstantInst(Context& /*context*/, SemIRLoc /*loc*/,
  289. SemIR::Temporary /*inst*/) -> ConstantEvalResult {
  290. // TODO: Handle this. Can we just return the value of `init_id`?
  291. return ConstantEvalResult::TODO;
  292. }
  293. auto EvalConstantInst(Context& context, SemIRLoc /*loc*/,
  294. SemIR::TupleAccess inst) -> ConstantEvalResult {
  295. return PerformAggregateAccess(context, inst);
  296. }
  297. auto EvalConstantInst(Context& /*context*/, SemIRLoc /*loc*/,
  298. SemIR::TupleInit inst) -> ConstantEvalResult {
  299. return ConstantEvalResult::NewSamePhase(SemIR::TupleValue{
  300. .type_id = inst.type_id, .elements_id = inst.elements_id});
  301. }
  302. auto EvalConstantInst(Context& context, SemIRLoc /*loc*/,
  303. SemIR::UnaryOperatorNot inst) -> ConstantEvalResult {
  304. // `not true` -> `false`, `not false` -> `true`.
  305. // All other uses of unary `not` are non-constant.
  306. auto const_id = context.constant_values().Get(inst.operand_id);
  307. if (const_id.is_concrete()) {
  308. auto value = context.insts().GetAs<SemIR::BoolLiteral>(
  309. context.constant_values().GetInstId(const_id));
  310. value.value = SemIR::BoolValue::From(!value.value.ToBool());
  311. return ConstantEvalResult::NewSamePhase(value);
  312. }
  313. return ConstantEvalResult::NotConstant;
  314. }
  315. auto EvalConstantInst(Context& context, SemIRLoc /*loc*/,
  316. SemIR::ValueOfInitializer inst) -> ConstantEvalResult {
  317. // Values of value expressions and initializing expressions are represented in
  318. // the same way during constant evaluation, so just return the value of the
  319. // operand.
  320. return ConstantEvalResult::Existing(
  321. context.constant_values().Get(inst.init_id));
  322. }
  323. auto EvalConstantInst(Context& context, SemIRLoc /*loc*/,
  324. SemIR::ValueParamPattern inst) -> ConstantEvalResult {
  325. // TODO: Treat this as a non-expression (here and in GetExprCategory)
  326. // once generic deduction doesn't need patterns to have constant values.
  327. return ConstantEvalResult::Existing(
  328. context.constant_values().Get(inst.subpattern_id));
  329. }
  330. auto EvalConstantInst(Context& /*context*/, SemIRLoc /*loc*/,
  331. SemIR::VtablePtr /*inst*/) -> ConstantEvalResult {
  332. // TODO: Handle this.
  333. return ConstantEvalResult::TODO;
  334. }
  335. } // namespace Carbon::Check