eval.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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. CARBON_CHECK(constant_id.is_symbolic());
  39. return Phase::Symbolic;
  40. }
  41. }
  42. // Returns the later of two phases.
  43. static auto LatestPhase(Phase a, Phase b) -> Phase {
  44. return static_cast<Phase>(
  45. std::max(static_cast<uint8_t>(a), static_cast<uint8_t>(b)));
  46. }
  47. // Forms a `constant_id` describing a given evaluation result.
  48. static auto MakeConstantResult(Context& context, SemIR::Inst inst, Phase phase)
  49. -> SemIR::ConstantId {
  50. switch (phase) {
  51. case Phase::Template:
  52. return context.AddConstant(inst, /*is_symbolic=*/false);
  53. case Phase::Symbolic:
  54. return context.AddConstant(inst, /*is_symbolic=*/true);
  55. case Phase::UnknownDueToError:
  56. return SemIR::ConstantId::Error;
  57. case Phase::Runtime:
  58. return SemIR::ConstantId::NotConstant;
  59. }
  60. }
  61. // Forms a `constant_id` describing why an evaluation was not constant.
  62. static auto MakeNonConstantResult(Phase phase) -> SemIR::ConstantId {
  63. return phase == Phase::UnknownDueToError ? SemIR::ConstantId::Error
  64. : SemIR::ConstantId::NotConstant;
  65. }
  66. // `GetConstantValue` checks to see whether the provided ID describes a value
  67. // with constant phase, and if so, returns the corresponding constant value.
  68. // Overloads are provided for different kinds of ID.
  69. // If the given instruction is constant, returns its constant value.
  70. static auto GetConstantValue(Context& context, SemIR::InstId inst_id,
  71. Phase* phase) -> SemIR::InstId {
  72. auto const_id = context.constant_values().Get(inst_id);
  73. *phase = LatestPhase(*phase, GetPhase(const_id));
  74. return const_id.inst_id();
  75. }
  76. // A type is always constant, but we still need to extract its phase.
  77. static auto GetConstantValue(Context& context, SemIR::TypeId type_id,
  78. Phase* phase) -> SemIR::TypeId {
  79. auto const_id = context.types().GetConstantId(type_id);
  80. *phase = LatestPhase(*phase, GetPhase(const_id));
  81. return type_id;
  82. }
  83. // If the given instruction block contains only constants, returns a
  84. // corresponding block of those values.
  85. static auto GetConstantValue(Context& context, SemIR::InstBlockId inst_block_id,
  86. Phase* phase) -> SemIR::InstBlockId {
  87. auto insts = context.inst_blocks().Get(inst_block_id);
  88. llvm::SmallVector<SemIR::InstId> const_insts;
  89. for (auto inst_id : insts) {
  90. auto const_inst_id = GetConstantValue(context, inst_id, phase);
  91. if (!const_inst_id.is_valid()) {
  92. return SemIR::InstBlockId::Invalid;
  93. }
  94. // Once we leave the small buffer, we know the first few elements are all
  95. // constant, so it's likely that the entire block is constant. Resize to the
  96. // target size given that we're going to allocate memory now anyway.
  97. if (const_insts.size() == const_insts.capacity()) {
  98. const_insts.reserve(insts.size());
  99. }
  100. const_insts.push_back(const_inst_id);
  101. }
  102. // TODO: If the new block is identical to the original block, return the
  103. // original ID.
  104. return context.inst_blocks().Add(const_insts);
  105. }
  106. // The constant value of a type block is that type block, but we still need to
  107. // extract its phase.
  108. static auto GetConstantValue(Context& context, SemIR::TypeBlockId type_block_id,
  109. Phase* phase) -> SemIR::TypeBlockId {
  110. auto types = context.type_blocks().Get(type_block_id);
  111. for (auto type_id : types) {
  112. GetConstantValue(context, type_id, phase);
  113. }
  114. return type_block_id;
  115. }
  116. // Replaces the specified field of the given typed instruction with its constant
  117. // value, if it has constant phase. Returns true on success, false if the value
  118. // has runtime phase.
  119. template <typename InstT, typename FieldIdT>
  120. static auto ReplaceFieldWithConstantValue(Context& context, InstT* inst,
  121. FieldIdT InstT::*field, Phase* phase)
  122. -> bool {
  123. auto unwrapped = GetConstantValue(context, inst->*field, phase);
  124. if (!unwrapped.is_valid()) {
  125. return false;
  126. }
  127. inst->*field = unwrapped;
  128. return true;
  129. }
  130. // If the specified fields of the given typed instruction have constant values,
  131. // replaces the fields with their constant values and builds a corresponding
  132. // constant value. Otherwise returns `ConstantId::NotConstant`. Returns
  133. // `ConstantId::Error` if any subexpression is an error.
  134. //
  135. // The constant value is then checked by calling `validate_fn(typed_inst)`,
  136. // which should return a `bool` indicating whether the new constant is valid. If
  137. // validation passes, a corresponding ConstantId for the new constant is
  138. // returned. If validation fails, it should produce a suitable error message.
  139. // `ConstantId::Error` is returned.
  140. template <typename InstT, typename ValidateFn, typename... EachFieldIdT>
  141. static auto RebuildAndValidateIfFieldsAreConstant(
  142. Context& context, SemIR::Inst inst, ValidateFn validate_fn,
  143. EachFieldIdT InstT::*... each_field_id) -> SemIR::ConstantId {
  144. // Build a constant instruction by replacing each non-constant operand with
  145. // its constant value.
  146. auto typed_inst = inst.As<InstT>();
  147. Phase phase = Phase::Template;
  148. if ((ReplaceFieldWithConstantValue(context, &typed_inst, each_field_id,
  149. &phase) &&
  150. ...)) {
  151. if (!validate_fn(typed_inst)) {
  152. return SemIR::ConstantId::Error;
  153. }
  154. return MakeConstantResult(context, typed_inst, phase);
  155. }
  156. return MakeNonConstantResult(phase);
  157. }
  158. // Same as above but with no validation step.
  159. template <typename InstT, typename... EachFieldIdT>
  160. static auto RebuildIfFieldsAreConstant(Context& context, SemIR::Inst inst,
  161. EachFieldIdT InstT::*... each_field_id)
  162. -> SemIR::ConstantId {
  163. return RebuildAndValidateIfFieldsAreConstant(
  164. context, inst, [](...) { return true; }, each_field_id...);
  165. }
  166. // Rebuilds the given aggregate initialization instruction as a corresponding
  167. // constant aggregate value, if its elements are all constants.
  168. static auto RebuildInitAsValue(Context& context, SemIR::Inst inst,
  169. SemIR::InstKind value_kind)
  170. -> SemIR::ConstantId {
  171. auto init_inst = inst.As<SemIR::AnyAggregateInit>();
  172. Phase phase = Phase::Template;
  173. auto elements_id = GetConstantValue(context, init_inst.elements_id, &phase);
  174. return MakeConstantResult(
  175. context,
  176. SemIR::AnyAggregateValue{.kind = value_kind,
  177. .type_id = init_inst.type_id,
  178. .elements_id = elements_id},
  179. phase);
  180. }
  181. // Performs an access into an aggregate, retrieving the specified element.
  182. static auto PerformAggregateAccess(Context& context, SemIR::Inst inst)
  183. -> SemIR::ConstantId {
  184. auto access_inst = inst.As<SemIR::AnyAggregateAccess>();
  185. Phase phase = Phase::Template;
  186. if (auto aggregate_id =
  187. GetConstantValue(context, access_inst.aggregate_id, &phase);
  188. aggregate_id.is_valid()) {
  189. if (auto aggregate =
  190. context.insts().TryGetAs<SemIR::AnyAggregateValue>(aggregate_id)) {
  191. auto elements = context.inst_blocks().Get(aggregate->elements_id);
  192. auto index = static_cast<size_t>(access_inst.index.index);
  193. CARBON_CHECK(index < elements.size()) << "Access out of bounds.";
  194. // `Phase` is not used here. If this element is a template constant, then
  195. // so is the result of indexing, even if the aggregate also contains a
  196. // symbolic context.
  197. return context.constant_values().Get(elements[index]);
  198. } else {
  199. CARBON_CHECK(phase != Phase::Template)
  200. << "Failed to evaluate template constant " << inst;
  201. }
  202. }
  203. return MakeNonConstantResult(phase);
  204. }
  205. // Performs an index into a homogeneous aggregate, retrieving the specified
  206. // element.
  207. static auto PerformAggregateIndex(Context& context, SemIR::Inst inst)
  208. -> SemIR::ConstantId {
  209. auto index_inst = inst.As<SemIR::AnyAggregateIndex>();
  210. Phase phase = Phase::Template;
  211. auto aggregate_id =
  212. GetConstantValue(context, index_inst.aggregate_id, &phase);
  213. auto index_id = GetConstantValue(context, index_inst.index_id, &phase);
  214. if (!index_id.is_valid()) {
  215. return MakeNonConstantResult(phase);
  216. }
  217. auto index = context.insts().TryGetAs<SemIR::IntLiteral>(index_id);
  218. if (!index) {
  219. CARBON_CHECK(phase != Phase::Template)
  220. << "Template constant integer should be a literal";
  221. return MakeNonConstantResult(phase);
  222. }
  223. // Array indexing is invalid if the index is constant and out of range.
  224. auto aggregate_type_id =
  225. context.insts().Get(index_inst.aggregate_id).type_id();
  226. const auto& index_val = context.ints().Get(index->int_id);
  227. if (auto array_type =
  228. context.types().TryGetAs<SemIR::ArrayType>(aggregate_type_id)) {
  229. if (auto bound =
  230. context.insts().TryGetAs<SemIR::IntLiteral>(array_type->bound_id)) {
  231. // This awkward call to `getZExtValue` is a workaround for APInt not
  232. // supporting comparisons between integers of different bit widths.
  233. if (index_val.getActiveBits() > 64 ||
  234. context.ints().Get(bound->int_id).ule(index_val.getZExtValue())) {
  235. CARBON_DIAGNOSTIC(ArrayIndexOutOfBounds, Error,
  236. "Array index `{0}` is past the end of type `{1}`.",
  237. llvm::APSInt, SemIR::TypeId);
  238. context.emitter().Emit(index_inst.index_id, ArrayIndexOutOfBounds,
  239. llvm::APSInt(index_val, /*isUnsigned=*/true),
  240. 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 semantic wrappers don't change the constant value.
  386. case SemIR::BindAlias::Kind:
  387. return context.constant_values().Get(
  388. inst.As<SemIR::BindAlias>().value_id);
  389. case SemIR::NameRef::Kind:
  390. return context.constant_values().Get(inst.As<SemIR::NameRef>().value_id);
  391. case SemIR::Converted::Kind:
  392. return context.constant_values().Get(
  393. inst.As<SemIR::Converted>().result_id);
  394. case SemIR::InitializeFrom::Kind:
  395. return context.constant_values().Get(
  396. inst.As<SemIR::InitializeFrom>().src_id);
  397. case SemIR::SpliceBlock::Kind:
  398. return context.constant_values().Get(
  399. inst.As<SemIR::SpliceBlock>().result_id);
  400. case SemIR::ValueOfInitializer::Kind:
  401. return context.constant_values().Get(
  402. inst.As<SemIR::ValueOfInitializer>().init_id);
  403. // `not true` -> `false`, `not false` -> `true`.
  404. // All other uses of unary `not` are non-constant.
  405. case SemIR::UnaryOperatorNot::Kind: {
  406. auto const_id = context.constant_values().Get(
  407. inst.As<SemIR::UnaryOperatorNot>().operand_id);
  408. auto phase = GetPhase(const_id);
  409. if (phase == Phase::Template) {
  410. auto value =
  411. context.insts().GetAs<SemIR::BoolLiteral>(const_id.inst_id());
  412. value.value =
  413. (value.value == SemIR::BoolValue::False ? SemIR::BoolValue::True
  414. : SemIR::BoolValue::False);
  415. return MakeConstantResult(context, value, Phase::Template);
  416. }
  417. if (phase == Phase::UnknownDueToError) {
  418. return SemIR::ConstantId::Error;
  419. }
  420. break;
  421. }
  422. // `const (const T)` evaluates to `const T`. Otherwise, `const T` evaluates
  423. // to itself.
  424. case SemIR::ConstType::Kind: {
  425. auto inner_id = context.constant_values().Get(
  426. context.types().GetInstId(inst.As<SemIR::ConstType>().inner_id));
  427. if (inner_id.is_constant() &&
  428. context.insts().Get(inner_id.inst_id()).Is<SemIR::ConstType>()) {
  429. return inner_id;
  430. }
  431. return MakeConstantResult(context, inst, GetPhase(inner_id));
  432. }
  433. // These cases are either not expressions or not constant.
  434. case SemIR::AddrPattern::Kind:
  435. case SemIR::Assign::Kind:
  436. case SemIR::BindName::Kind:
  437. case SemIR::BlockArg::Kind:
  438. case SemIR::Branch::Kind:
  439. case SemIR::BranchIf::Kind:
  440. case SemIR::BranchWithArg::Kind:
  441. case SemIR::ClassDecl::Kind:
  442. case SemIR::ImplDecl::Kind:
  443. case SemIR::Import::Kind:
  444. case SemIR::InterfaceDecl::Kind:
  445. case SemIR::Param::Kind:
  446. case SemIR::ReturnExpr::Kind:
  447. case SemIR::Return::Kind:
  448. case SemIR::StructLiteral::Kind:
  449. case SemIR::TupleLiteral::Kind:
  450. case SemIR::VarStorage::Kind:
  451. break;
  452. case SemIR::ImportRefUnused::Kind:
  453. CARBON_FATAL() << "ImportRefUnused should transform to ImportRefUsed "
  454. "before TryEvalInst.";
  455. }
  456. return SemIR::ConstantId::NotConstant;
  457. }
  458. } // namespace Carbon::Check