eval_inst.cpp 16 KB

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