eval.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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.h"
  5. #include "toolchain/sem_ir/ids.h"
  6. #include "toolchain/sem_ir/typed_insts.h"
  7. #include "toolchain/sem_ir/value_stores.h"
  8. namespace Carbon::Check {
  9. namespace {
  10. // The evaluation phase for an expression, computed by evaluation. These are
  11. // ordered so that the phase of an expression is the numerically highest phase
  12. // of its constituent evaluations. Note that an expression with any runtime
  13. // component is known to have Runtime phase even if it involves an evaluation
  14. // with UnknownDueToError phase.
  15. enum class Phase : uint8_t {
  16. // Value could be entirely and concretely computed.
  17. Template,
  18. // Evaluation phase is symbolic because the expression involves a reference to
  19. // a symbolic binding.
  20. Symbolic,
  21. // The evaluation phase is unknown because evaluation encountered an
  22. // already-diagnosed semantic or syntax error. This is treated as being
  23. // potentially constant, but with an unknown phase.
  24. UnknownDueToError,
  25. // The expression has runtime phase because of a non-constant subexpression.
  26. Runtime,
  27. };
  28. } // namespace
  29. // Gets the phase in which the value of a constant will become available.
  30. static auto GetPhase(SemIR::ConstantId constant_id) -> Phase {
  31. if (!constant_id.is_constant()) {
  32. return Phase::Runtime;
  33. } else if (constant_id == SemIR::ConstantId::Error) {
  34. return Phase::UnknownDueToError;
  35. } else if (constant_id.is_template()) {
  36. return Phase::Template;
  37. } else {
  38. return Phase::Symbolic;
  39. }
  40. }
  41. // Returns the later of two phases.
  42. static auto LatestPhase(Phase a, Phase b) -> Phase {
  43. return static_cast<Phase>(
  44. std::max(static_cast<uint8_t>(a), static_cast<uint8_t>(b)));
  45. }
  46. // Forms a `constant_id` describing a given evaluation result.
  47. static auto MakeConstantResult(Context& context, SemIR::Inst inst, Phase phase)
  48. -> SemIR::ConstantId {
  49. switch (phase) {
  50. case Phase::Template:
  51. return context.AddConstant(inst, /*is_symbolic=*/false);
  52. case Phase::Symbolic:
  53. return context.AddConstant(inst, /*is_symbolic=*/true);
  54. case Phase::UnknownDueToError:
  55. return SemIR::ConstantId::Error;
  56. case Phase::Runtime:
  57. return SemIR::ConstantId::NotConstant;
  58. }
  59. }
  60. // Forms a `constant_id` describing why an evaluation was not constant.
  61. static auto MakeNonConstantResult(Phase phase) -> SemIR::ConstantId {
  62. return phase == Phase::UnknownDueToError ? SemIR::ConstantId::Error
  63. : SemIR::ConstantId::NotConstant;
  64. }
  65. // `GetConstantValue` checks to see whether the provided ID describes a value
  66. // with constant phase, and if so, returns the corresponding constant value.
  67. // Overloads are provided for different kinds of ID.
  68. // If the given instruction is constant, returns its constant value.
  69. static auto GetConstantValue(Context& context, SemIR::InstId inst_id,
  70. Phase* phase) -> SemIR::InstId {
  71. auto const_id = context.constant_values().Get(inst_id);
  72. *phase = LatestPhase(*phase, GetPhase(const_id));
  73. return const_id.inst_id();
  74. }
  75. // A type is always constant, but we still need to extract its phase.
  76. static auto GetConstantValue(Context& context, SemIR::TypeId type_id,
  77. Phase* phase) -> SemIR::TypeId {
  78. auto const_id = context.types().GetConstantId(type_id);
  79. *phase = LatestPhase(*phase, GetPhase(const_id));
  80. return type_id;
  81. }
  82. // If the given instruction block contains only constants, returns a
  83. // corresponding block of those values.
  84. static auto GetConstantValue(Context& context, SemIR::InstBlockId inst_block_id,
  85. Phase* phase) -> SemIR::InstBlockId {
  86. auto insts = context.inst_blocks().Get(inst_block_id);
  87. llvm::SmallVector<SemIR::InstId> const_insts;
  88. for (auto inst_id : insts) {
  89. auto const_inst_id = GetConstantValue(context, inst_id, phase);
  90. if (!const_inst_id.is_valid()) {
  91. return SemIR::InstBlockId::Invalid;
  92. }
  93. // Once we leave the small buffer, we know the first few elements are all
  94. // constant, so it's likely that the entire block is constant. Resize to the
  95. // target size given that we're going to allocate memory now anyway.
  96. if (const_insts.size() == const_insts.capacity()) {
  97. const_insts.reserve(insts.size());
  98. }
  99. const_insts.push_back(const_inst_id);
  100. }
  101. // TODO: If the new block is identical to the original block, return the
  102. // original ID.
  103. return context.inst_blocks().Add(const_insts);
  104. }
  105. // The constant value of a type block is that type block, but we still need to
  106. // extract its phase.
  107. static auto GetConstantValue(Context& context, SemIR::TypeBlockId type_block_id,
  108. Phase* phase) -> SemIR::TypeBlockId {
  109. auto types = context.type_blocks().Get(type_block_id);
  110. for (auto type_id : types) {
  111. GetConstantValue(context, type_id, phase);
  112. }
  113. return type_block_id;
  114. }
  115. // Replaces the specified field of the given typed instruction with its constant
  116. // value, if it has constant phase. Returns true on success, false if the value
  117. // has runtime phase.
  118. template <typename InstT, typename FieldIdT>
  119. static auto ReplaceFieldWithConstantValue(Context& context, InstT* inst,
  120. FieldIdT InstT::*field, Phase* phase)
  121. -> bool {
  122. auto unwrapped = GetConstantValue(context, inst->*field, phase);
  123. if (!unwrapped.is_valid()) {
  124. return false;
  125. }
  126. inst->*field = unwrapped;
  127. return true;
  128. }
  129. // If the specified fields of the given typed instruction have constant values,
  130. // replaces the fields with their constant values and builds a corresponding
  131. // constant value. Otherwise returns `ConstantId::NotConstant`. Returns
  132. // `ConstantId::Error` if any subexpression is an error.
  133. //
  134. // The constant value is then checked by calling `validate_fn(typed_inst)`,
  135. // which should return a `bool` indicating whether the new constant is valid. If
  136. // validation passes, a corresponding ConstantId for the new constant is
  137. // returned. If validation fails, it should produce a suitable error message.
  138. // `ConstantId::Error` is returned.
  139. template <typename InstT, typename ValidateFn, typename... EachFieldIdT>
  140. static auto RebuildAndValidateIfFieldsAreConstant(
  141. Context& context, SemIR::Inst inst, ValidateFn validate_fn,
  142. EachFieldIdT InstT::*... each_field_id) -> SemIR::ConstantId {
  143. // Build a constant instruction by replacing each non-constant operand with
  144. // its constant value.
  145. auto typed_inst = inst.As<InstT>();
  146. Phase phase = Phase::Template;
  147. if ((ReplaceFieldWithConstantValue(context, &typed_inst, each_field_id,
  148. &phase) &&
  149. ...)) {
  150. if (!validate_fn(typed_inst)) {
  151. return SemIR::ConstantId::Error;
  152. }
  153. return MakeConstantResult(context, typed_inst, phase);
  154. }
  155. return MakeNonConstantResult(phase);
  156. }
  157. // Same as above but with no validation step.
  158. template <typename InstT, typename... EachFieldIdT>
  159. static auto RebuildIfFieldsAreConstant(Context& context, SemIR::Inst inst,
  160. EachFieldIdT InstT::*... each_field_id)
  161. -> SemIR::ConstantId {
  162. return RebuildAndValidateIfFieldsAreConstant(
  163. context, inst, [](...) { return true; }, each_field_id...);
  164. }
  165. // Rebuilds the given aggregate initialization instruction as a corresponding
  166. // constant aggregate value, if its elements are all constants.
  167. static auto RebuildInitAsValue(Context& context, SemIR::Inst inst,
  168. SemIR::InstKind value_kind)
  169. -> SemIR::ConstantId {
  170. auto init_inst = inst.As<SemIR::AnyAggregateInit>();
  171. Phase phase = Phase::Template;
  172. auto elements_id = GetConstantValue(context, init_inst.elements_id, &phase);
  173. return MakeConstantResult(
  174. context,
  175. SemIR::AnyAggregateValue{.kind = value_kind,
  176. .type_id = init_inst.type_id,
  177. .elements_id = elements_id},
  178. phase);
  179. }
  180. // Performs an access into an aggregate, retrieving the specified element.
  181. static auto PerformAggregateAccess(Context& context, SemIR::Inst inst)
  182. -> SemIR::ConstantId {
  183. auto access_inst = inst.As<SemIR::AnyAggregateAccess>();
  184. Phase phase = Phase::Template;
  185. if (auto aggregate_id =
  186. GetConstantValue(context, access_inst.aggregate_id, &phase);
  187. aggregate_id.is_valid()) {
  188. if (auto aggregate =
  189. context.insts().TryGetAs<SemIR::AnyAggregateValue>(aggregate_id)) {
  190. auto elements = context.inst_blocks().Get(aggregate->elements_id);
  191. auto index = static_cast<size_t>(access_inst.index.index);
  192. CARBON_CHECK(index < elements.size()) << "Access out of bounds.";
  193. // `Phase` is not used here. If this element is a template constant, then
  194. // so is the result of indexing, even if the aggregate also contains a
  195. // symbolic context.
  196. return context.constant_values().Get(elements[index]);
  197. } else {
  198. CARBON_CHECK(phase != Phase::Template)
  199. << "Failed to evaluate template constant " << inst;
  200. }
  201. }
  202. return MakeNonConstantResult(phase);
  203. }
  204. // Performs an index into a homogeneous aggregate, retrieving the specified
  205. // element.
  206. static auto PerformAggregateIndex(Context& context, SemIR::Inst inst)
  207. -> SemIR::ConstantId {
  208. auto index_inst = inst.As<SemIR::AnyAggregateIndex>();
  209. Phase phase = Phase::Template;
  210. auto aggregate_id =
  211. GetConstantValue(context, index_inst.aggregate_id, &phase);
  212. auto index_id = GetConstantValue(context, index_inst.index_id, &phase);
  213. if (!index_id.is_valid()) {
  214. return MakeNonConstantResult(phase);
  215. }
  216. auto index = context.insts().TryGetAs<SemIR::IntLiteral>(index_id);
  217. if (!index) {
  218. CARBON_CHECK(phase != Phase::Template)
  219. << "Template constant integer should be a literal";
  220. return MakeNonConstantResult(phase);
  221. }
  222. // Array indexing is invalid if the index is constant and out of range.
  223. auto aggregate_type_id =
  224. context.insts().Get(index_inst.aggregate_id).type_id();
  225. const auto& index_val = context.ints().Get(index->int_id);
  226. if (auto array_type =
  227. context.types().TryGetAs<SemIR::ArrayType>(aggregate_type_id)) {
  228. if (auto bound =
  229. context.insts().TryGetAs<SemIR::IntLiteral>(array_type->bound_id)) {
  230. // This awkward call to `getZExtValue` is a workaround for APInt not
  231. // supporting comparisons between integers of different bit widths.
  232. if (index_val.getActiveBits() > 64 ||
  233. context.ints().Get(bound->int_id).ule(index_val.getZExtValue())) {
  234. CARBON_DIAGNOSTIC(ArrayIndexOutOfBounds, Error,
  235. "Array index `{0}` is past the end of type `{1}`.",
  236. llvm::APSInt, std::string);
  237. context.emitter().Emit(
  238. index_inst.index_id, ArrayIndexOutOfBounds,
  239. llvm::APSInt(index_val, /*isUnsigned=*/true),
  240. context.sem_ir().StringifyType(aggregate_type_id));
  241. return SemIR::ConstantId::Error;
  242. }
  243. }
  244. }
  245. if (!aggregate_id.is_valid()) {
  246. return MakeNonConstantResult(phase);
  247. }
  248. auto aggregate =
  249. context.insts().TryGetAs<SemIR::AnyAggregateValue>(aggregate_id);
  250. if (!aggregate) {
  251. CARBON_CHECK(phase != Phase::Template)
  252. << "Unexpected representation for template constant aggregate";
  253. return MakeNonConstantResult(phase);
  254. }
  255. auto elements = context.inst_blocks().Get(aggregate->elements_id);
  256. // We checked this for the array case above.
  257. CARBON_CHECK(index_val.ult(elements.size()))
  258. << "Index out of bounds in tuple indexing";
  259. return context.constant_values().Get(elements[index_val.getZExtValue()]);
  260. }
  261. auto TryEvalInst(Context& context, SemIR::InstId inst_id, SemIR::Inst inst)
  262. -> SemIR::ConstantId {
  263. // TODO: Ensure we have test coverage for each of these cases that can result
  264. // in a constant, once those situations are all reachable.
  265. switch (inst.kind()) {
  266. // These cases are constants if their operands are.
  267. case SemIR::AddrOf::Kind:
  268. return RebuildIfFieldsAreConstant(context, inst,
  269. &SemIR::AddrOf::lvalue_id);
  270. case SemIR::ArrayType::Kind:
  271. return RebuildAndValidateIfFieldsAreConstant(
  272. context, inst,
  273. [&](SemIR::ArrayType result) {
  274. auto bound_id = inst.As<SemIR::ArrayType>().bound_id;
  275. auto int_bound =
  276. context.insts().TryGetAs<SemIR::IntLiteral>(result.bound_id);
  277. if (!int_bound) {
  278. // TODO: Permit symbolic array bounds. This will require fixing
  279. // callers of `GetArrayBoundValue`.
  280. context.TODO(context.insts().GetParseNode(bound_id),
  281. "symbolic array bound");
  282. return false;
  283. }
  284. // TODO: We should check that the size of the resulting array type
  285. // fits in 64 bits, not just that the bound does. Should we use a
  286. // 32-bit limit for 32-bit targets?
  287. // TODO: Also check for a negative bound, once that's something we
  288. // can represent.
  289. const auto& bound_val = context.ints().Get(int_bound->int_id);
  290. if (bound_val.getActiveBits() > 64) {
  291. CARBON_DIAGNOSTIC(ArrayBoundTooLarge, Error,
  292. "Array bound of {0} is too large.",
  293. llvm::APInt);
  294. context.emitter().Emit(bound_id, ArrayBoundTooLarge, bound_val);
  295. return false;
  296. }
  297. return true;
  298. },
  299. &SemIR::ArrayType::bound_id, &SemIR::ArrayType::element_type_id);
  300. case SemIR::BoundMethod::Kind:
  301. return RebuildIfFieldsAreConstant(context, inst,
  302. &SemIR::BoundMethod::object_id,
  303. &SemIR::BoundMethod::function_id);
  304. case SemIR::PointerType::Kind:
  305. return RebuildIfFieldsAreConstant(context, inst,
  306. &SemIR::PointerType::pointee_id);
  307. case SemIR::StructType::Kind:
  308. return RebuildIfFieldsAreConstant(context, inst,
  309. &SemIR::StructType::fields_id);
  310. case SemIR::StructTypeField::Kind:
  311. return RebuildIfFieldsAreConstant(context, inst,
  312. &SemIR::StructTypeField::field_type_id);
  313. case SemIR::StructValue::Kind:
  314. return RebuildIfFieldsAreConstant(context, inst,
  315. &SemIR::StructValue::elements_id);
  316. case SemIR::TupleType::Kind:
  317. return RebuildIfFieldsAreConstant(context, inst,
  318. &SemIR::TupleType::elements_id);
  319. case SemIR::TupleValue::Kind:
  320. return RebuildIfFieldsAreConstant(context, inst,
  321. &SemIR::TupleValue::elements_id);
  322. case SemIR::UnboundElementType::Kind:
  323. return RebuildIfFieldsAreConstant(
  324. context, inst, &SemIR::UnboundElementType::class_type_id,
  325. &SemIR::UnboundElementType::element_type_id);
  326. // Initializers evaluate to a value of the object representation.
  327. case SemIR::ArrayInit::Kind:
  328. // TODO: Add an `ArrayValue` to represent a constant array object
  329. // representation instead of using a `TupleValue`.
  330. return RebuildInitAsValue(context, inst, SemIR::TupleValue::Kind);
  331. case SemIR::ClassInit::Kind:
  332. // TODO: Add a `ClassValue` to represent a constant class object
  333. // representation instead of using a `StructValue`.
  334. return RebuildInitAsValue(context, inst, SemIR::StructValue::Kind);
  335. case SemIR::StructInit::Kind:
  336. return RebuildInitAsValue(context, inst, SemIR::StructValue::Kind);
  337. case SemIR::TupleInit::Kind:
  338. return RebuildInitAsValue(context, inst, SemIR::TupleValue::Kind);
  339. // These cases are always template constants.
  340. case SemIR::Builtin::Kind:
  341. case SemIR::ClassType::Kind:
  342. case SemIR::InterfaceType::Kind:
  343. // TODO: Once classes and interfaces have generic arguments, handle them.
  344. return MakeConstantResult(context, inst, Phase::Template);
  345. // These cases are treated as being the unique canonical definition of the
  346. // corresponding constant value.
  347. // TODO: This doesn't properly handle redeclarations. Consider adding a
  348. // corresponding `Value` inst for each of these cases.
  349. case SemIR::BaseDecl::Kind:
  350. case SemIR::FieldDecl::Kind:
  351. case SemIR::FunctionDecl::Kind:
  352. case SemIR::Namespace::Kind:
  353. return SemIR::ConstantId::ForTemplateConstant(inst_id);
  354. case SemIR::BoolLiteral::Kind:
  355. case SemIR::IntLiteral::Kind:
  356. case SemIR::RealLiteral::Kind:
  357. case SemIR::StringLiteral::Kind:
  358. // Promote literals to the constant block.
  359. // TODO: Convert literals into a canonical form. Currently we can form two
  360. // different `i32` constants with the same value if they are represented
  361. // by `APInt`s with different bit widths.
  362. return MakeConstantResult(context, inst, Phase::Template);
  363. // The elements of a constant aggregate can be accessed.
  364. case SemIR::ClassElementAccess::Kind:
  365. case SemIR::StructAccess::Kind:
  366. case SemIR::TupleAccess::Kind:
  367. return PerformAggregateAccess(context, inst);
  368. case SemIR::ArrayIndex::Kind:
  369. case SemIR::TupleIndex::Kind:
  370. return PerformAggregateIndex(context, inst);
  371. // TODO: These need special handling.
  372. case SemIR::BindValue::Kind:
  373. case SemIR::Call::Kind:
  374. case SemIR::Deref::Kind:
  375. case SemIR::ImportRefUsed::Kind:
  376. case SemIR::Temporary::Kind:
  377. case SemIR::TemporaryStorage::Kind:
  378. case SemIR::ValueAsRef::Kind:
  379. break;
  380. case SemIR::BindSymbolicName::Kind:
  381. // TODO: Consider forming a constant value here using a de Bruijn index or
  382. // similar, so that corresponding symbolic parameters in redeclarations
  383. // are treated as the same value.
  384. return SemIR::ConstantId::ForSymbolicConstant(inst_id);
  385. // These semnatic wrappers don't change the constant value.
  386. case SemIR::NameRef::Kind:
  387. return context.constant_values().Get(inst.As<SemIR::NameRef>().value_id);
  388. case SemIR::Converted::Kind:
  389. return context.constant_values().Get(
  390. inst.As<SemIR::Converted>().result_id);
  391. case SemIR::InitializeFrom::Kind:
  392. return context.constant_values().Get(
  393. inst.As<SemIR::InitializeFrom>().src_id);
  394. case SemIR::SpliceBlock::Kind:
  395. return context.constant_values().Get(
  396. inst.As<SemIR::SpliceBlock>().result_id);
  397. case SemIR::ValueOfInitializer::Kind:
  398. return context.constant_values().Get(
  399. inst.As<SemIR::ValueOfInitializer>().init_id);
  400. // `not true` -> `false`, `not false` -> `true`.
  401. // All other uses of unary `not` are non-constant.
  402. case SemIR::UnaryOperatorNot::Kind: {
  403. auto const_id = context.constant_values().Get(
  404. inst.As<SemIR::UnaryOperatorNot>().operand_id);
  405. auto phase = GetPhase(const_id);
  406. if (phase == Phase::Template) {
  407. auto value =
  408. context.insts().GetAs<SemIR::BoolLiteral>(const_id.inst_id());
  409. value.value =
  410. (value.value == SemIR::BoolValue::False ? SemIR::BoolValue::True
  411. : SemIR::BoolValue::False);
  412. return MakeConstantResult(context, value, Phase::Template);
  413. }
  414. if (phase == Phase::UnknownDueToError) {
  415. return SemIR::ConstantId::Error;
  416. }
  417. break;
  418. }
  419. // `const (const T)` evaluates to `const T`. Otherwise, `const T` evaluates
  420. // to itself.
  421. case SemIR::ConstType::Kind: {
  422. auto inner_id = context.constant_values().Get(
  423. context.types().GetInstId(inst.As<SemIR::ConstType>().inner_id));
  424. if (inner_id.is_constant() &&
  425. context.insts().Get(inner_id.inst_id()).Is<SemIR::ConstType>()) {
  426. return inner_id;
  427. }
  428. return MakeConstantResult(context, inst, GetPhase(inner_id));
  429. }
  430. // These cases are either not expressions or not constant.
  431. case SemIR::AddrPattern::Kind:
  432. case SemIR::Assign::Kind:
  433. case SemIR::BindName::Kind:
  434. case SemIR::BlockArg::Kind:
  435. case SemIR::Branch::Kind:
  436. case SemIR::BranchIf::Kind:
  437. case SemIR::BranchWithArg::Kind:
  438. case SemIR::ClassDecl::Kind:
  439. case SemIR::Import::Kind:
  440. case SemIR::InterfaceDecl::Kind:
  441. case SemIR::Param::Kind:
  442. case SemIR::ReturnExpr::Kind:
  443. case SemIR::Return::Kind:
  444. case SemIR::StructLiteral::Kind:
  445. case SemIR::TupleLiteral::Kind:
  446. case SemIR::VarStorage::Kind:
  447. break;
  448. case SemIR::ImportRefUnused::Kind:
  449. CARBON_FATAL() << "ImportRefUnused should transform to ImportRefUsed "
  450. "before TryEvalInst.";
  451. }
  452. return SemIR::ConstantId::NotConstant;
  453. }
  454. } // namespace Carbon::Check