eval.cpp 89 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148
  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/base/kind_switch.h"
  6. #include "toolchain/check/diagnostic_helpers.h"
  7. #include "toolchain/check/generic.h"
  8. #include "toolchain/check/import_ref.h"
  9. #include "toolchain/diagnostics/diagnostic_emitter.h"
  10. #include "toolchain/diagnostics/format_providers.h"
  11. #include "toolchain/sem_ir/builtin_function_kind.h"
  12. #include "toolchain/sem_ir/function.h"
  13. #include "toolchain/sem_ir/generic.h"
  14. #include "toolchain/sem_ir/ids.h"
  15. #include "toolchain/sem_ir/inst_kind.h"
  16. #include "toolchain/sem_ir/typed_insts.h"
  17. namespace Carbon::Check {
  18. namespace {
  19. // Information about an eval block of a specific that we are currently building.
  20. struct SpecificEvalInfo {
  21. // The region within the specific whose eval block we are building.
  22. SemIR::GenericInstIndex::Region region;
  23. // The work-in-progress contents of the eval block.
  24. llvm::ArrayRef<SemIR::InstId> values;
  25. };
  26. // Information about the context within which we are performing evaluation.
  27. class EvalContext {
  28. public:
  29. explicit EvalContext(
  30. Context& context, SemIRLoc fallback_loc,
  31. SemIR::SpecificId specific_id = SemIR::SpecificId::None,
  32. std::optional<SpecificEvalInfo> specific_eval_info = std::nullopt)
  33. : context_(context),
  34. fallback_loc_(fallback_loc),
  35. specific_id_(specific_id),
  36. specific_eval_info_(specific_eval_info) {}
  37. // Gets the location to use for diagnostics if a better location is
  38. // unavailable.
  39. // TODO: This is also sometimes unavailable.
  40. auto fallback_loc() const -> SemIRLoc { return fallback_loc_; }
  41. // Returns a location to use to point at an instruction in a diagnostic, given
  42. // a list of instructions that might have an attached location. This is the
  43. // location of the first instruction in the list that has a location if there
  44. // is one, and otherwise the fallback location.
  45. auto GetDiagnosticLoc(llvm::ArrayRef<SemIR::InstId> inst_ids) -> SemIRLoc {
  46. for (auto inst_id : inst_ids) {
  47. if (inst_id.has_value() &&
  48. context_.insts().GetLocId(inst_id).has_value()) {
  49. return inst_id;
  50. }
  51. }
  52. return fallback_loc_;
  53. }
  54. // Gets the value of the specified compile-time binding in this context.
  55. // Returns `None` if the value is not fixed in this context.
  56. auto GetCompileTimeBindValue(SemIR::CompileTimeBindIndex bind_index)
  57. -> SemIR::ConstantId {
  58. if (!bind_index.has_value() || !specific_id_.has_value()) {
  59. return SemIR::ConstantId::None;
  60. }
  61. const auto& specific = specifics().Get(specific_id_);
  62. auto args = inst_blocks().Get(specific.args_id);
  63. // Bindings past the ones with known arguments can appear as local
  64. // bindings of entities declared within this generic.
  65. if (static_cast<size_t>(bind_index.index) >= args.size()) {
  66. return SemIR::ConstantId::None;
  67. }
  68. return constant_values().Get(args[bind_index.index]);
  69. }
  70. // Given a constant value from the SemIR we're evaluating, finds the
  71. // corresponding constant value to use in the context of this evaluation.
  72. // This can be different if the original SemIR is for a generic and we are
  73. // evaluating with specific arguments for the generic parameters.
  74. auto GetInContext(SemIR::ConstantId const_id) -> SemIR::ConstantId {
  75. if (!const_id.is_symbolic()) {
  76. return const_id;
  77. }
  78. // While resolving a specific, map from previous instructions in the eval
  79. // block into their evaluated values. These values won't be present on the
  80. // specific itself yet, so `GetConstantInSpecific` won't be able to find
  81. // them.
  82. if (specific_eval_info_) {
  83. const auto& symbolic_info =
  84. constant_values().GetSymbolicConstant(const_id);
  85. if (symbolic_info.index.has_value() &&
  86. symbolic_info.generic_id ==
  87. specifics().Get(specific_id_).generic_id &&
  88. symbolic_info.index.region() == specific_eval_info_->region) {
  89. auto inst_id = specific_eval_info_->values[symbolic_info.index.index()];
  90. CARBON_CHECK(inst_id.has_value(),
  91. "Forward reference in eval block: index {0} referenced "
  92. "before evaluation",
  93. symbolic_info.index.index());
  94. return constant_values().Get(inst_id);
  95. }
  96. }
  97. // Map from a specific constant value to the canonical value.
  98. return GetConstantInSpecific(sem_ir(), specific_id_, const_id);
  99. }
  100. // Gets the constant value of the specified instruction in this context.
  101. auto GetConstantValue(SemIR::InstId inst_id) -> SemIR::ConstantId {
  102. return GetInContext(constant_values().Get(inst_id));
  103. }
  104. // Gets the constant value of the specified type in this context.
  105. auto GetConstantValue(SemIR::TypeId type_id) -> SemIR::ConstantId {
  106. return GetInContext(types().GetConstantId(type_id));
  107. }
  108. // Gets the constant value of the specified type in this context.
  109. auto GetConstantValueAsType(SemIR::TypeId id) -> SemIR::TypeId {
  110. return context().GetTypeIdForTypeConstant(GetConstantValue(id));
  111. }
  112. // Gets the instruction describing the constant value of the specified type in
  113. // this context.
  114. auto GetConstantValueAsInst(SemIR::TypeId id) -> SemIR::Inst {
  115. return insts().Get(
  116. context().constant_values().GetInstId(GetConstantValue(id)));
  117. }
  118. auto ints() -> SharedValueStores::IntStore& { return sem_ir().ints(); }
  119. auto floats() -> SharedValueStores::FloatStore& { return sem_ir().floats(); }
  120. auto entity_names() -> SemIR::EntityNameStore& {
  121. return sem_ir().entity_names();
  122. }
  123. auto functions() -> const ValueStore<SemIR::FunctionId>& {
  124. return sem_ir().functions();
  125. }
  126. auto classes() -> const ValueStore<SemIR::ClassId>& {
  127. return sem_ir().classes();
  128. }
  129. auto interfaces() -> const ValueStore<SemIR::InterfaceId>& {
  130. return sem_ir().interfaces();
  131. }
  132. auto facet_types() -> CanonicalValueStore<SemIR::FacetTypeId>& {
  133. return sem_ir().facet_types();
  134. }
  135. auto specifics() -> const SemIR::SpecificStore& {
  136. return sem_ir().specifics();
  137. }
  138. auto type_blocks() -> SemIR::BlockValueStore<SemIR::TypeBlockId>& {
  139. return sem_ir().type_blocks();
  140. }
  141. auto insts() -> const SemIR::InstStore& { return sem_ir().insts(); }
  142. auto inst_blocks() -> SemIR::InstBlockStore& {
  143. return sem_ir().inst_blocks();
  144. }
  145. // Gets the constant value store. Note that this does not provide the constant
  146. // values that should be used from this evaluation context, and so should be
  147. // used with caution.
  148. auto constant_values() -> const SemIR::ConstantValueStore& {
  149. return sem_ir().constant_values();
  150. }
  151. // Gets the types store. Note that this does not provide the type values that
  152. // should be used from this evaluation context, and so should be used with
  153. // caution.
  154. auto types() -> const SemIR::TypeStore& { return sem_ir().types(); }
  155. auto context() -> Context& { return context_; }
  156. auto sem_ir() -> SemIR::File& { return context().sem_ir(); }
  157. auto emitter() -> Context::DiagnosticEmitter& { return context().emitter(); }
  158. private:
  159. // The type-checking context in which we're performing evaluation.
  160. Context& context_;
  161. // The location to use for diagnostics when a better location isn't available.
  162. SemIRLoc fallback_loc_;
  163. // The specific that we are evaluating within.
  164. SemIR::SpecificId specific_id_;
  165. // If we are currently evaluating an eval block for `specific_id_`,
  166. // information about that evaluation.
  167. std::optional<SpecificEvalInfo> specific_eval_info_;
  168. };
  169. } // namespace
  170. namespace {
  171. // The evaluation phase for an expression, computed by evaluation. These are
  172. // ordered so that the phase of an expression is the numerically highest phase
  173. // of its constituent evaluations. Note that an expression with any runtime
  174. // component is known to have Runtime phase even if it involves an evaluation
  175. // with UnknownDueToError phase.
  176. enum class Phase : uint8_t {
  177. // Value could be entirely and concretely computed.
  178. Template,
  179. // Evaluation phase is symbolic because the expression involves specifically a
  180. // reference to `.Self`.
  181. PeriodSelfSymbolic,
  182. // Evaluation phase is symbolic because the expression involves a reference to
  183. // a symbolic binding.
  184. Symbolic,
  185. // The evaluation phase is unknown because evaluation encountered an
  186. // already-diagnosed semantic or syntax error. This is treated as being
  187. // potentially constant, but with an unknown phase.
  188. UnknownDueToError,
  189. // The expression has runtime phase because of a non-constant subexpression.
  190. Runtime,
  191. };
  192. } // namespace
  193. // Gets the phase in which the value of a constant will become available.
  194. static auto GetPhase(EvalContext& eval_context, SemIR::ConstantId constant_id)
  195. -> Phase {
  196. if (!constant_id.is_constant()) {
  197. return Phase::Runtime;
  198. } else if (constant_id == SemIR::ErrorInst::SingletonConstantId) {
  199. return Phase::UnknownDueToError;
  200. } else if (constant_id.is_template()) {
  201. return Phase::Template;
  202. } else if (eval_context.constant_values().DependsOnGenericParameter(
  203. constant_id)) {
  204. return Phase::Symbolic;
  205. } else {
  206. CARBON_CHECK(constant_id.is_symbolic());
  207. return Phase::PeriodSelfSymbolic;
  208. }
  209. }
  210. // Returns the later of two phases.
  211. static auto LatestPhase(Phase a, Phase b) -> Phase {
  212. return static_cast<Phase>(
  213. std::max(static_cast<uint8_t>(a), static_cast<uint8_t>(b)));
  214. }
  215. // `where` expressions using `.Self` should not be considered symbolic
  216. // - `Interface where .Self impls I and .A = bool` -> template
  217. // - `T:! type` ... `Interface where .A = T` -> symbolic, since uses `T` which
  218. // is symbolic and not due to `.Self`.
  219. static auto UpdatePhaseIgnorePeriodSelf(EvalContext& eval_context,
  220. SemIR::ConstantId constant_id,
  221. Phase* phase) {
  222. Phase constant_phase = GetPhase(eval_context, constant_id);
  223. // Since LatestPhase(x, Phase::Template) == x, this is equivalent to replacing
  224. // Phase::PeriodSelfSymbolic with Phase::Template.
  225. if (constant_phase != Phase::PeriodSelfSymbolic) {
  226. *phase = LatestPhase(*phase, constant_phase);
  227. }
  228. }
  229. // Forms a `constant_id` describing a given evaluation result.
  230. static auto MakeConstantResult(Context& context, SemIR::Inst inst, Phase phase)
  231. -> SemIR::ConstantId {
  232. switch (phase) {
  233. case Phase::Template:
  234. return context.constants().GetOrAdd(inst,
  235. SemIR::ConstantStore::IsTemplate);
  236. case Phase::PeriodSelfSymbolic:
  237. return context.constants().GetOrAdd(
  238. inst, SemIR::ConstantStore::IsPeriodSelfSymbolic);
  239. case Phase::Symbolic:
  240. return context.constants().GetOrAdd(inst,
  241. SemIR::ConstantStore::IsSymbolic);
  242. case Phase::UnknownDueToError:
  243. return SemIR::ErrorInst::SingletonConstantId;
  244. case Phase::Runtime:
  245. return SemIR::ConstantId::NotConstant;
  246. }
  247. }
  248. // Forms a `constant_id` describing why an evaluation was not constant.
  249. static auto MakeNonConstantResult(Phase phase) -> SemIR::ConstantId {
  250. return phase == Phase::UnknownDueToError
  251. ? SemIR::ErrorInst::SingletonConstantId
  252. : SemIR::ConstantId::NotConstant;
  253. }
  254. // Converts a bool value into a ConstantId.
  255. static auto MakeBoolResult(Context& context, SemIR::TypeId bool_type_id,
  256. bool result) -> SemIR::ConstantId {
  257. return MakeConstantResult(
  258. context,
  259. SemIR::BoolLiteral{.type_id = bool_type_id,
  260. .value = SemIR::BoolValue::From(result)},
  261. Phase::Template);
  262. }
  263. // Converts an APInt value into a ConstantId.
  264. static auto MakeIntResult(Context& context, SemIR::TypeId type_id,
  265. bool is_signed, llvm::APInt value)
  266. -> SemIR::ConstantId {
  267. CARBON_CHECK(is_signed == context.types().IsSignedInt(type_id));
  268. auto result = is_signed ? context.ints().AddSigned(std::move(value))
  269. : context.ints().AddUnsigned(std::move(value));
  270. return MakeConstantResult(
  271. context, SemIR::IntValue{.type_id = type_id, .int_id = result},
  272. Phase::Template);
  273. }
  274. // Converts an APFloat value into a ConstantId.
  275. static auto MakeFloatResult(Context& context, SemIR::TypeId type_id,
  276. llvm::APFloat value) -> SemIR::ConstantId {
  277. auto result = context.floats().Add(std::move(value));
  278. return MakeConstantResult(
  279. context, SemIR::FloatLiteral{.type_id = type_id, .float_id = result},
  280. Phase::Template);
  281. }
  282. // `GetConstantValue` checks to see whether the provided ID describes a value
  283. // with constant phase, and if so, returns the corresponding constant value.
  284. // Overloads are provided for different kinds of ID.
  285. // If the given instruction is constant, returns its constant value.
  286. static auto GetConstantValue(EvalContext& eval_context, SemIR::InstId inst_id,
  287. Phase* phase) -> SemIR::InstId {
  288. auto const_id = eval_context.GetConstantValue(inst_id);
  289. *phase = LatestPhase(*phase, GetPhase(eval_context, const_id));
  290. return eval_context.constant_values().GetInstId(const_id);
  291. }
  292. // Given a type which may refer to a generic parameter, returns the
  293. // corresponding type in the evaluation context.
  294. static auto GetConstantValue(EvalContext& eval_context, SemIR::TypeId type_id,
  295. Phase* phase) -> SemIR::TypeId {
  296. auto const_id = eval_context.GetConstantValue(type_id);
  297. *phase = LatestPhase(*phase, GetPhase(eval_context, const_id));
  298. return eval_context.context().GetTypeIdForTypeConstant(const_id);
  299. }
  300. // If the given instruction block contains only constants, returns a
  301. // corresponding block of those values.
  302. static auto GetConstantValue(EvalContext& eval_context,
  303. SemIR::InstBlockId inst_block_id, Phase* phase)
  304. -> SemIR::InstBlockId {
  305. if (!inst_block_id.has_value()) {
  306. return SemIR::InstBlockId::None;
  307. }
  308. auto insts = eval_context.inst_blocks().Get(inst_block_id);
  309. llvm::SmallVector<SemIR::InstId> const_insts;
  310. for (auto inst_id : insts) {
  311. auto const_inst_id = GetConstantValue(eval_context, inst_id, phase);
  312. if (!const_inst_id.has_value()) {
  313. return SemIR::InstBlockId::None;
  314. }
  315. // Once we leave the small buffer, we know the first few elements are all
  316. // constant, so it's likely that the entire block is constant. Resize to the
  317. // target size given that we're going to allocate memory now anyway.
  318. if (const_insts.size() == const_insts.capacity()) {
  319. const_insts.reserve(insts.size());
  320. }
  321. const_insts.push_back(const_inst_id);
  322. }
  323. // TODO: If the new block is identical to the original block, and we know the
  324. // old ID was canonical, return the original ID.
  325. return eval_context.inst_blocks().AddCanonical(const_insts);
  326. }
  327. // Compute the constant value of a type block. This may be different from the
  328. // input type block if we have known generic arguments.
  329. static auto GetConstantValue(EvalContext& eval_context,
  330. SemIR::StructTypeFieldsId fields_id, Phase* phase)
  331. -> SemIR::StructTypeFieldsId {
  332. if (!fields_id.has_value()) {
  333. return SemIR::StructTypeFieldsId::None;
  334. }
  335. auto fields = eval_context.context().struct_type_fields().Get(fields_id);
  336. llvm::SmallVector<SemIR::StructTypeField> new_fields;
  337. for (auto field : fields) {
  338. auto new_type_id = GetConstantValue(eval_context, field.type_id, phase);
  339. if (!new_type_id.has_value()) {
  340. return SemIR::StructTypeFieldsId::None;
  341. }
  342. // Once we leave the small buffer, we know the first few elements are all
  343. // constant, so it's likely that the entire block is constant. Resize to the
  344. // target size given that we're going to allocate memory now anyway.
  345. if (new_fields.size() == new_fields.capacity()) {
  346. new_fields.reserve(fields.size());
  347. }
  348. new_fields.push_back({.name_id = field.name_id, .type_id = new_type_id});
  349. }
  350. // TODO: If the new block is identical to the original block, and we know the
  351. // old ID was canonical, return the original ID.
  352. return eval_context.context().struct_type_fields().AddCanonical(new_fields);
  353. }
  354. // Compute the constant value of a type block. This may be different from the
  355. // input type block if we have known generic arguments.
  356. static auto GetConstantValue(EvalContext& eval_context,
  357. SemIR::TypeBlockId type_block_id, Phase* phase)
  358. -> SemIR::TypeBlockId {
  359. if (!type_block_id.has_value()) {
  360. return SemIR::TypeBlockId::None;
  361. }
  362. auto types = eval_context.type_blocks().Get(type_block_id);
  363. llvm::SmallVector<SemIR::TypeId> new_types;
  364. for (auto type_id : types) {
  365. auto new_type_id = GetConstantValue(eval_context, type_id, phase);
  366. if (!new_type_id.has_value()) {
  367. return SemIR::TypeBlockId::None;
  368. }
  369. // Once we leave the small buffer, we know the first few elements are all
  370. // constant, so it's likely that the entire block is constant. Resize to the
  371. // target size given that we're going to allocate memory now anyway.
  372. if (new_types.size() == new_types.capacity()) {
  373. new_types.reserve(types.size());
  374. }
  375. new_types.push_back(new_type_id);
  376. }
  377. // TODO: If the new block is identical to the original block, and we know the
  378. // old ID was canonical, return the original ID.
  379. return eval_context.type_blocks().AddCanonical(new_types);
  380. }
  381. // The constant value of a specific is the specific with the corresponding
  382. // constant values for its arguments.
  383. static auto GetConstantValue(EvalContext& eval_context,
  384. SemIR::SpecificId specific_id, Phase* phase)
  385. -> SemIR::SpecificId {
  386. if (!specific_id.has_value()) {
  387. return SemIR::SpecificId::None;
  388. }
  389. const auto& specific = eval_context.specifics().Get(specific_id);
  390. auto args_id = GetConstantValue(eval_context, specific.args_id, phase);
  391. if (!args_id.has_value()) {
  392. return SemIR::SpecificId::None;
  393. }
  394. if (args_id == specific.args_id) {
  395. return specific_id;
  396. }
  397. return MakeSpecific(eval_context.context(), eval_context.fallback_loc(),
  398. specific.generic_id, args_id);
  399. }
  400. // Like `GetConstantValue` but does a `FacetTypeId` -> `FacetTypeInfo`
  401. // conversion. Does not perform canonicalization.
  402. static auto GetConstantFacetTypeInfo(EvalContext& eval_context,
  403. SemIR::FacetTypeId facet_type_id,
  404. Phase* phase) -> SemIR::FacetTypeInfo {
  405. SemIR::FacetTypeInfo info = eval_context.facet_types().Get(facet_type_id);
  406. for (auto& interface : info.impls_constraints) {
  407. interface.specific_id =
  408. GetConstantValue(eval_context, interface.specific_id, phase);
  409. }
  410. for (auto& rewrite : info.rewrite_constraints) {
  411. rewrite.lhs_const_id = eval_context.GetInContext(rewrite.lhs_const_id);
  412. rewrite.rhs_const_id = eval_context.GetInContext(rewrite.rhs_const_id);
  413. // `where` requirements using `.Self` should not be considered symbolic
  414. UpdatePhaseIgnorePeriodSelf(eval_context, rewrite.lhs_const_id, phase);
  415. UpdatePhaseIgnorePeriodSelf(eval_context, rewrite.rhs_const_id, phase);
  416. }
  417. // TODO: Process other requirements.
  418. return info;
  419. }
  420. // Replaces the specified field of the given typed instruction with its constant
  421. // value, if it has constant phase. Returns true on success, false if the value
  422. // has runtime phase.
  423. template <typename InstT, typename FieldIdT>
  424. static auto ReplaceFieldWithConstantValue(EvalContext& eval_context,
  425. InstT* inst, FieldIdT InstT::*field,
  426. Phase* phase) -> bool {
  427. auto unwrapped = GetConstantValue(eval_context, inst->*field, phase);
  428. if (!unwrapped.has_value() && (inst->*field).has_value()) {
  429. return false;
  430. }
  431. inst->*field = unwrapped;
  432. return true;
  433. }
  434. // If the specified fields of the given typed instruction have constant values,
  435. // replaces the fields with their constant values and builds a corresponding
  436. // constant value. Otherwise returns `ConstantId::NotConstant`. Returns
  437. // `ErrorInst::SingletonConstantId` if any subexpression is an error.
  438. //
  439. // The constant value is then checked by calling `validate_fn(typed_inst)`,
  440. // which should return a `bool` indicating whether the new constant is valid. If
  441. // validation passes, `transform_fn(typed_inst)` is called to produce the final
  442. // constant instruction, and a corresponding ConstantId for the new constant is
  443. // returned. If validation fails, it should produce a suitable error message.
  444. // `ErrorInst::SingletonConstantId` is returned.
  445. template <typename InstT, typename ValidateFn, typename TransformFn,
  446. typename... EachFieldIdT>
  447. static auto RebuildIfFieldsAreConstantImpl(
  448. EvalContext& eval_context, SemIR::Inst inst, ValidateFn validate_fn,
  449. TransformFn transform_fn, EachFieldIdT InstT::*... each_field_id)
  450. -> SemIR::ConstantId {
  451. // Build a constant instruction by replacing each non-constant operand with
  452. // its constant value.
  453. auto typed_inst = inst.As<InstT>();
  454. Phase phase = Phase::Template;
  455. if ((ReplaceFieldWithConstantValue(eval_context, &typed_inst, each_field_id,
  456. &phase) &&
  457. ...)) {
  458. if (phase == Phase::UnknownDueToError || !validate_fn(typed_inst)) {
  459. return SemIR::ErrorInst::SingletonConstantId;
  460. }
  461. return MakeConstantResult(eval_context.context(), transform_fn(typed_inst),
  462. phase);
  463. }
  464. return MakeNonConstantResult(phase);
  465. }
  466. // Same as above but with an identity transform function.
  467. template <typename InstT, typename ValidateFn, typename... EachFieldIdT>
  468. static auto RebuildAndValidateIfFieldsAreConstant(
  469. EvalContext& eval_context, SemIR::Inst inst, ValidateFn validate_fn,
  470. EachFieldIdT InstT::*... each_field_id) -> SemIR::ConstantId {
  471. return RebuildIfFieldsAreConstantImpl(eval_context, inst, validate_fn,
  472. std::identity{}, each_field_id...);
  473. }
  474. // Same as above but with no validation step.
  475. template <typename InstT, typename TransformFn, typename... EachFieldIdT>
  476. static auto TransformIfFieldsAreConstant(EvalContext& eval_context,
  477. SemIR::Inst inst,
  478. TransformFn transform_fn,
  479. EachFieldIdT InstT::*... each_field_id)
  480. -> SemIR::ConstantId {
  481. return RebuildIfFieldsAreConstantImpl(
  482. eval_context, inst, [](...) { return true; }, transform_fn,
  483. each_field_id...);
  484. }
  485. // Same as above but with no validation or transform step.
  486. template <typename InstT, typename... EachFieldIdT>
  487. static auto RebuildIfFieldsAreConstant(EvalContext& eval_context,
  488. SemIR::Inst inst,
  489. EachFieldIdT InstT::*... each_field_id)
  490. -> SemIR::ConstantId {
  491. return RebuildIfFieldsAreConstantImpl(
  492. eval_context, inst, [](...) { return true; }, std::identity{},
  493. each_field_id...);
  494. }
  495. // Rebuilds the given aggregate initialization instruction as a corresponding
  496. // constant aggregate value, if its elements are all constants.
  497. static auto RebuildInitAsValue(EvalContext& eval_context, SemIR::Inst inst,
  498. SemIR::InstKind value_kind)
  499. -> SemIR::ConstantId {
  500. return TransformIfFieldsAreConstant(
  501. eval_context, inst,
  502. [&](SemIR::AnyAggregateInit result) {
  503. return SemIR::AnyAggregateValue{.kind = value_kind,
  504. .type_id = result.type_id,
  505. .elements_id = result.elements_id};
  506. },
  507. &SemIR::AnyAggregateInit::type_id, &SemIR::AnyAggregateInit::elements_id);
  508. }
  509. // Performs an access into an aggregate, retrieving the specified element.
  510. static auto PerformAggregateAccess(EvalContext& eval_context, SemIR::Inst inst)
  511. -> SemIR::ConstantId {
  512. auto access_inst = inst.As<SemIR::AnyAggregateAccess>();
  513. Phase phase = Phase::Template;
  514. if (ReplaceFieldWithConstantValue(eval_context, &access_inst,
  515. &SemIR::AnyAggregateAccess::aggregate_id,
  516. &phase)) {
  517. if (auto aggregate =
  518. eval_context.insts().TryGetAs<SemIR::AnyAggregateValue>(
  519. access_inst.aggregate_id)) {
  520. auto elements = eval_context.inst_blocks().Get(aggregate->elements_id);
  521. auto index = static_cast<size_t>(access_inst.index.index);
  522. CARBON_CHECK(index < elements.size(), "Access out of bounds.");
  523. // `Phase` is not used here. If this element is a template constant, then
  524. // so is the result of indexing, even if the aggregate also contains a
  525. // symbolic context.
  526. return eval_context.GetConstantValue(elements[index]);
  527. } else {
  528. CARBON_CHECK(phase != Phase::Template,
  529. "Failed to evaluate template constant {0} arg0: {1}", inst,
  530. eval_context.insts().Get(access_inst.aggregate_id));
  531. }
  532. return MakeConstantResult(eval_context.context(), access_inst, phase);
  533. }
  534. return MakeNonConstantResult(phase);
  535. }
  536. // Performs an index into a homogeneous aggregate, retrieving the specified
  537. // element.
  538. static auto PerformArrayIndex(EvalContext& eval_context, SemIR::ArrayIndex inst)
  539. -> SemIR::ConstantId {
  540. Phase phase = Phase::Template;
  541. auto index_id = GetConstantValue(eval_context, inst.index_id, &phase);
  542. if (!index_id.has_value()) {
  543. return MakeNonConstantResult(phase);
  544. }
  545. auto index = eval_context.insts().TryGetAs<SemIR::IntValue>(index_id);
  546. if (!index) {
  547. CARBON_CHECK(phase != Phase::Template,
  548. "Template constant integer should be a literal");
  549. return MakeNonConstantResult(phase);
  550. }
  551. // Array indexing is invalid if the index is constant and out of range,
  552. // regardless of whether the array itself is constant.
  553. const auto& index_val = eval_context.ints().Get(index->int_id);
  554. auto aggregate_type_id = eval_context.GetConstantValueAsType(
  555. eval_context.insts().Get(inst.array_id).type_id());
  556. if (auto array_type =
  557. eval_context.types().TryGetAs<SemIR::ArrayType>(aggregate_type_id)) {
  558. if (auto bound = eval_context.insts().TryGetAs<SemIR::IntValue>(
  559. array_type->bound_id)) {
  560. // This awkward call to `getZExtValue` is a workaround for APInt not
  561. // supporting comparisons between integers of different bit widths.
  562. if (index_val.getActiveBits() > 64 ||
  563. eval_context.ints()
  564. .Get(bound->int_id)
  565. .ule(index_val.getZExtValue())) {
  566. CARBON_DIAGNOSTIC(ArrayIndexOutOfBounds, Error,
  567. "array index `{0}` is past the end of type {1}",
  568. TypedInt, SemIR::TypeId);
  569. eval_context.emitter().Emit(
  570. eval_context.GetDiagnosticLoc(inst.index_id), ArrayIndexOutOfBounds,
  571. {.type = index->type_id, .value = index_val}, aggregate_type_id);
  572. return SemIR::ErrorInst::SingletonConstantId;
  573. }
  574. }
  575. }
  576. auto aggregate_id = GetConstantValue(eval_context, inst.array_id, &phase);
  577. if (!aggregate_id.has_value()) {
  578. return MakeNonConstantResult(phase);
  579. }
  580. auto aggregate =
  581. eval_context.insts().TryGetAs<SemIR::AnyAggregateValue>(aggregate_id);
  582. if (!aggregate) {
  583. CARBON_CHECK(phase != Phase::Template,
  584. "Unexpected representation for template constant aggregate");
  585. return MakeNonConstantResult(phase);
  586. }
  587. auto elements = eval_context.inst_blocks().Get(aggregate->elements_id);
  588. return eval_context.GetConstantValue(elements[index_val.getZExtValue()]);
  589. }
  590. // Enforces that an integer type has a valid bit width.
  591. static auto ValidateIntType(Context& context, SemIRLoc loc,
  592. SemIR::IntType result) -> bool {
  593. auto bit_width =
  594. context.insts().TryGetAs<SemIR::IntValue>(result.bit_width_id);
  595. if (!bit_width) {
  596. // Symbolic bit width.
  597. return true;
  598. }
  599. const auto& bit_width_val = context.ints().Get(bit_width->int_id);
  600. if (bit_width_val.isZero() ||
  601. (context.types().IsSignedInt(bit_width->type_id) &&
  602. bit_width_val.isNegative())) {
  603. CARBON_DIAGNOSTIC(IntWidthNotPositive, Error,
  604. "integer type width of {0} is not positive", TypedInt);
  605. context.emitter().Emit(
  606. loc, IntWidthNotPositive,
  607. {.type = bit_width->type_id, .value = bit_width_val});
  608. return false;
  609. }
  610. if (bit_width_val.ugt(IntStore::MaxIntWidth)) {
  611. CARBON_DIAGNOSTIC(IntWidthTooLarge, Error,
  612. "integer type width of {0} is greater than the "
  613. "maximum supported width of {1}",
  614. TypedInt, int);
  615. context.emitter().Emit(loc, IntWidthTooLarge,
  616. {.type = bit_width->type_id, .value = bit_width_val},
  617. IntStore::MaxIntWidth);
  618. return false;
  619. }
  620. return true;
  621. }
  622. // Forms a constant int type as an evaluation result. Requires that width_id is
  623. // constant.
  624. static auto MakeIntTypeResult(Context& context, SemIRLoc loc,
  625. SemIR::IntKind int_kind, SemIR::InstId width_id,
  626. Phase phase) -> SemIR::ConstantId {
  627. auto result = SemIR::IntType{
  628. .type_id = context.GetSingletonType(SemIR::TypeType::SingletonInstId),
  629. .int_kind = int_kind,
  630. .bit_width_id = width_id};
  631. if (!ValidateIntType(context, loc, result)) {
  632. return SemIR::ErrorInst::SingletonConstantId;
  633. }
  634. return MakeConstantResult(context, result, phase);
  635. }
  636. // Enforces that the bit width is 64 for a float.
  637. static auto ValidateFloatBitWidth(Context& context, SemIRLoc loc,
  638. SemIR::InstId inst_id) -> bool {
  639. auto inst = context.insts().GetAs<SemIR::IntValue>(inst_id);
  640. if (context.ints().Get(inst.int_id) == 64) {
  641. return true;
  642. }
  643. CARBON_DIAGNOSTIC(CompileTimeFloatBitWidth, Error, "bit width must be 64");
  644. context.emitter().Emit(loc, CompileTimeFloatBitWidth);
  645. return false;
  646. }
  647. // Enforces that a float type has a valid bit width.
  648. static auto ValidateFloatType(Context& context, SemIRLoc loc,
  649. SemIR::FloatType result) -> bool {
  650. auto bit_width =
  651. context.insts().TryGetAs<SemIR::IntValue>(result.bit_width_id);
  652. if (!bit_width) {
  653. // Symbolic bit width.
  654. return true;
  655. }
  656. return ValidateFloatBitWidth(context, loc, result.bit_width_id);
  657. }
  658. // Performs a conversion between integer types, truncating if the value doesn't
  659. // fit in the destination type.
  660. static auto PerformIntConvert(Context& context, SemIR::InstId arg_id,
  661. SemIR::TypeId dest_type_id) -> SemIR::ConstantId {
  662. auto arg_val =
  663. context.ints().Get(context.insts().GetAs<SemIR::IntValue>(arg_id).int_id);
  664. auto [dest_is_signed, bit_width_id] =
  665. context.sem_ir().types().GetIntTypeInfo(dest_type_id);
  666. if (bit_width_id.has_value()) {
  667. // TODO: If the value fits in the destination type, reuse the existing
  668. // int_id rather than recomputing it. This is probably the most common case.
  669. bool src_is_signed = context.sem_ir().types().IsSignedInt(
  670. context.insts().Get(arg_id).type_id());
  671. unsigned width = context.ints().Get(bit_width_id).getZExtValue();
  672. arg_val =
  673. src_is_signed ? arg_val.sextOrTrunc(width) : arg_val.zextOrTrunc(width);
  674. }
  675. return MakeIntResult(context, dest_type_id, dest_is_signed, arg_val);
  676. }
  677. // Performs a conversion between integer types, diagnosing if the value doesn't
  678. // fit in the destination type.
  679. static auto PerformCheckedIntConvert(Context& context, SemIRLoc loc,
  680. SemIR::InstId arg_id,
  681. SemIR::TypeId dest_type_id)
  682. -> SemIR::ConstantId {
  683. auto arg = context.insts().GetAs<SemIR::IntValue>(arg_id);
  684. auto arg_val = context.ints().Get(arg.int_id);
  685. auto [is_signed, bit_width_id] =
  686. context.sem_ir().types().GetIntTypeInfo(dest_type_id);
  687. auto width = bit_width_id.has_value()
  688. ? context.ints().Get(bit_width_id).getZExtValue()
  689. : arg_val.getBitWidth();
  690. if (!is_signed && arg_val.isNegative()) {
  691. CARBON_DIAGNOSTIC(
  692. NegativeIntInUnsignedType, Error,
  693. "negative integer value {0} converted to unsigned type {1}", TypedInt,
  694. SemIR::TypeId);
  695. context.emitter().Emit(loc, NegativeIntInUnsignedType,
  696. {.type = arg.type_id, .value = arg_val},
  697. dest_type_id);
  698. }
  699. unsigned arg_non_sign_bits = arg_val.getSignificantBits() - 1;
  700. if (arg_non_sign_bits + is_signed > width) {
  701. CARBON_DIAGNOSTIC(IntTooLargeForType, Error,
  702. "integer value {0} too large for type {1}", TypedInt,
  703. SemIR::TypeId);
  704. context.emitter().Emit(loc, IntTooLargeForType,
  705. {.type = arg.type_id, .value = arg_val},
  706. dest_type_id);
  707. }
  708. return MakeConstantResult(
  709. context, SemIR::IntValue{.type_id = dest_type_id, .int_id = arg.int_id},
  710. Phase::Template);
  711. }
  712. // Issues a diagnostic for a compile-time division by zero.
  713. static auto DiagnoseDivisionByZero(Context& context, SemIRLoc loc) -> void {
  714. CARBON_DIAGNOSTIC(CompileTimeDivisionByZero, Error, "division by zero");
  715. context.emitter().Emit(loc, CompileTimeDivisionByZero);
  716. }
  717. // Get an integer at a suitable bit-width: either `bit_width_id` if it has a
  718. // value, or the canonical width from the value store if not.
  719. static auto GetIntAtSuitableWidth(Context& context, IntId int_id,
  720. IntId bit_width_id) -> llvm::APInt {
  721. return bit_width_id.has_value()
  722. ? context.ints().GetAtWidth(int_id, bit_width_id)
  723. : context.ints().Get(int_id);
  724. }
  725. // Performs a builtin unary integer -> integer operation.
  726. static auto PerformBuiltinUnaryIntOp(Context& context, SemIRLoc loc,
  727. SemIR::BuiltinFunctionKind builtin_kind,
  728. SemIR::InstId arg_id)
  729. -> SemIR::ConstantId {
  730. auto op = context.insts().GetAs<SemIR::IntValue>(arg_id);
  731. auto [is_signed, bit_width_id] =
  732. context.sem_ir().types().GetIntTypeInfo(op.type_id);
  733. llvm::APInt op_val = GetIntAtSuitableWidth(context, op.int_id, bit_width_id);
  734. switch (builtin_kind) {
  735. case SemIR::BuiltinFunctionKind::IntSNegate:
  736. if (op_val.isMinSignedValue()) {
  737. if (bit_width_id.has_value()) {
  738. CARBON_DIAGNOSTIC(CompileTimeIntegerNegateOverflow, Error,
  739. "integer overflow in negation of {0}", TypedInt);
  740. context.emitter().Emit(loc, CompileTimeIntegerNegateOverflow,
  741. {.type = op.type_id, .value = op_val});
  742. } else {
  743. // Widen the integer so we don't overflow into the sign bit.
  744. op_val = op_val.sext(op_val.getBitWidth() +
  745. llvm::APInt::APINT_BITS_PER_WORD);
  746. }
  747. }
  748. op_val.negate();
  749. break;
  750. case SemIR::BuiltinFunctionKind::IntUNegate:
  751. CARBON_CHECK(bit_width_id.has_value(), "Unsigned negate on unsized int");
  752. op_val.negate();
  753. break;
  754. case SemIR::BuiltinFunctionKind::IntComplement:
  755. // TODO: Should we have separate builtins for signed and unsigned
  756. // complement? Like with signed/unsigned negate, these operations do
  757. // different things to the integer value, even though they do the same
  758. // thing to the bits. We treat IntLiteral complement as signed complement,
  759. // given that the result of unsigned complement depends on the bit width.
  760. op_val.flipAllBits();
  761. break;
  762. default:
  763. CARBON_FATAL("Unexpected builtin kind");
  764. }
  765. return MakeIntResult(context, op.type_id, is_signed, std::move(op_val));
  766. }
  767. namespace {
  768. // A pair of APInts that are the operands of a binary operator. We use an
  769. // aggregate rather than `std::pair` to allow RVO of the individual ints.
  770. struct APIntBinaryOperands {
  771. llvm::APInt lhs;
  772. llvm::APInt rhs;
  773. };
  774. } // namespace
  775. // Get a pair of integers at the same suitable bit-width: either their actual
  776. // width if they have a fixed width, or the smallest canonical width in which
  777. // they both fit otherwise.
  778. static auto GetIntsAtSuitableWidth(Context& context, IntId lhs_id, IntId rhs_id,
  779. IntId bit_width_id) -> APIntBinaryOperands {
  780. // Unsized operands: take the wider of the bit widths.
  781. if (!bit_width_id.has_value()) {
  782. APIntBinaryOperands result = {.lhs = context.ints().Get(lhs_id),
  783. .rhs = context.ints().Get(rhs_id)};
  784. if (result.lhs.getBitWidth() != result.rhs.getBitWidth()) {
  785. if (result.lhs.getBitWidth() > result.rhs.getBitWidth()) {
  786. result.rhs = result.rhs.sext(result.lhs.getBitWidth());
  787. } else {
  788. result.lhs = result.lhs.sext(result.rhs.getBitWidth());
  789. }
  790. }
  791. return result;
  792. }
  793. return {.lhs = context.ints().GetAtWidth(lhs_id, bit_width_id),
  794. .rhs = context.ints().GetAtWidth(rhs_id, bit_width_id)};
  795. }
  796. namespace {
  797. // The result of performing a binary int operation.
  798. struct BinaryIntOpResult {
  799. llvm::APInt result_val;
  800. bool overflow;
  801. Lex::TokenKind op_token;
  802. };
  803. } // namespace
  804. // Computes the result of a homogeneous binary (int, int) -> int operation.
  805. static auto ComputeBinaryIntOpResult(SemIR::BuiltinFunctionKind builtin_kind,
  806. const llvm::APInt& lhs_val,
  807. const llvm::APInt& rhs_val)
  808. -> BinaryIntOpResult {
  809. llvm::APInt result_val;
  810. bool overflow = false;
  811. Lex::TokenKind op_token = Lex::TokenKind::Not;
  812. switch (builtin_kind) {
  813. // Arithmetic.
  814. case SemIR::BuiltinFunctionKind::IntSAdd:
  815. result_val = lhs_val.sadd_ov(rhs_val, overflow);
  816. op_token = Lex::TokenKind::Plus;
  817. break;
  818. case SemIR::BuiltinFunctionKind::IntSSub:
  819. result_val = lhs_val.ssub_ov(rhs_val, overflow);
  820. op_token = Lex::TokenKind::Minus;
  821. break;
  822. case SemIR::BuiltinFunctionKind::IntSMul:
  823. result_val = lhs_val.smul_ov(rhs_val, overflow);
  824. op_token = Lex::TokenKind::Star;
  825. break;
  826. case SemIR::BuiltinFunctionKind::IntSDiv:
  827. result_val = lhs_val.sdiv_ov(rhs_val, overflow);
  828. op_token = Lex::TokenKind::Slash;
  829. break;
  830. case SemIR::BuiltinFunctionKind::IntSMod:
  831. result_val = lhs_val.srem(rhs_val);
  832. // LLVM weirdly lacks `srem_ov`, so we work it out for ourselves:
  833. // <signed min> % -1 overflows because <signed min> / -1 overflows.
  834. overflow = lhs_val.isMinSignedValue() && rhs_val.isAllOnes();
  835. op_token = Lex::TokenKind::Percent;
  836. break;
  837. case SemIR::BuiltinFunctionKind::IntUAdd:
  838. result_val = lhs_val + rhs_val;
  839. op_token = Lex::TokenKind::Plus;
  840. break;
  841. case SemIR::BuiltinFunctionKind::IntUSub:
  842. result_val = lhs_val - rhs_val;
  843. op_token = Lex::TokenKind::Minus;
  844. break;
  845. case SemIR::BuiltinFunctionKind::IntUMul:
  846. result_val = lhs_val * rhs_val;
  847. op_token = Lex::TokenKind::Star;
  848. break;
  849. case SemIR::BuiltinFunctionKind::IntUDiv:
  850. result_val = lhs_val.udiv(rhs_val);
  851. op_token = Lex::TokenKind::Slash;
  852. break;
  853. case SemIR::BuiltinFunctionKind::IntUMod:
  854. result_val = lhs_val.urem(rhs_val);
  855. op_token = Lex::TokenKind::Percent;
  856. break;
  857. // Bitwise.
  858. case SemIR::BuiltinFunctionKind::IntAnd:
  859. result_val = lhs_val & rhs_val;
  860. op_token = Lex::TokenKind::And;
  861. break;
  862. case SemIR::BuiltinFunctionKind::IntOr:
  863. result_val = lhs_val | rhs_val;
  864. op_token = Lex::TokenKind::Pipe;
  865. break;
  866. case SemIR::BuiltinFunctionKind::IntXor:
  867. result_val = lhs_val ^ rhs_val;
  868. op_token = Lex::TokenKind::Caret;
  869. break;
  870. case SemIR::BuiltinFunctionKind::IntLeftShift:
  871. case SemIR::BuiltinFunctionKind::IntRightShift:
  872. CARBON_FATAL("Non-homogeneous operation handled separately.");
  873. default:
  874. CARBON_FATAL("Unexpected operation kind.");
  875. }
  876. return {.result_val = std::move(result_val),
  877. .overflow = overflow,
  878. .op_token = op_token};
  879. }
  880. // Performs a builtin integer bit shift operation.
  881. static auto PerformBuiltinIntShiftOp(Context& context, SemIRLoc loc,
  882. SemIR::BuiltinFunctionKind builtin_kind,
  883. SemIR::InstId lhs_id, SemIR::InstId rhs_id)
  884. -> SemIR::ConstantId {
  885. auto lhs = context.insts().GetAs<SemIR::IntValue>(lhs_id);
  886. auto rhs = context.insts().GetAs<SemIR::IntValue>(rhs_id);
  887. auto [lhs_is_signed, lhs_bit_width_id] =
  888. context.sem_ir().types().GetIntTypeInfo(lhs.type_id);
  889. llvm::APInt lhs_val =
  890. GetIntAtSuitableWidth(context, lhs.int_id, lhs_bit_width_id);
  891. const auto& rhs_orig_val = context.ints().Get(rhs.int_id);
  892. if (lhs_bit_width_id.has_value() && rhs_orig_val.uge(lhs_val.getBitWidth())) {
  893. CARBON_DIAGNOSTIC(
  894. CompileTimeShiftOutOfRange, Error,
  895. "shift distance >= type width of {0} in `{1} {2:<<|>>} {3}`", unsigned,
  896. TypedInt, BoolAsSelect, TypedInt);
  897. context.emitter().Emit(
  898. loc, CompileTimeShiftOutOfRange, lhs_val.getBitWidth(),
  899. {.type = lhs.type_id, .value = lhs_val},
  900. builtin_kind == SemIR::BuiltinFunctionKind::IntLeftShift,
  901. {.type = rhs.type_id, .value = rhs_orig_val});
  902. // TODO: Is it useful to recover by returning 0 or -1?
  903. return SemIR::ErrorInst::SingletonConstantId;
  904. }
  905. if (rhs_orig_val.isNegative() &&
  906. context.sem_ir().types().IsSignedInt(rhs.type_id)) {
  907. CARBON_DIAGNOSTIC(CompileTimeShiftNegative, Error,
  908. "shift distance negative in `{0} {1:<<|>>} {2}`",
  909. TypedInt, BoolAsSelect, TypedInt);
  910. context.emitter().Emit(
  911. loc, CompileTimeShiftNegative, {.type = lhs.type_id, .value = lhs_val},
  912. builtin_kind == SemIR::BuiltinFunctionKind::IntLeftShift,
  913. {.type = rhs.type_id, .value = rhs_orig_val});
  914. // TODO: Is it useful to recover by returning 0 or -1?
  915. return SemIR::ErrorInst::SingletonConstantId;
  916. }
  917. llvm::APInt result_val;
  918. if (builtin_kind == SemIR::BuiltinFunctionKind::IntLeftShift) {
  919. if (!lhs_bit_width_id.has_value() && !lhs_val.isZero()) {
  920. // Ensure we don't generate a ridiculously large integer through a bit
  921. // shift.
  922. auto width = rhs_orig_val.trySExtValue();
  923. if (!width ||
  924. *width > IntStore::MaxIntWidth - lhs_val.getSignificantBits()) {
  925. CARBON_DIAGNOSTIC(CompileTimeUnsizedShiftOutOfRange, Error,
  926. "shift distance of {0} would result in an "
  927. "integer whose width is greater than the "
  928. "maximum supported width of {1}",
  929. TypedInt, int);
  930. context.emitter().Emit(loc, CompileTimeUnsizedShiftOutOfRange,
  931. {.type = rhs.type_id, .value = rhs_orig_val},
  932. IntStore::MaxIntWidth);
  933. return SemIR::ErrorInst::SingletonConstantId;
  934. }
  935. lhs_val = lhs_val.sext(
  936. IntStore::CanonicalBitWidth(lhs_val.getSignificantBits() + *width));
  937. }
  938. result_val =
  939. lhs_val.shl(rhs_orig_val.getLimitedValue(lhs_val.getBitWidth()));
  940. } else if (lhs_is_signed) {
  941. result_val =
  942. lhs_val.ashr(rhs_orig_val.getLimitedValue(lhs_val.getBitWidth()));
  943. } else {
  944. CARBON_CHECK(lhs_bit_width_id.has_value(), "Logical shift on unsized int");
  945. result_val =
  946. lhs_val.lshr(rhs_orig_val.getLimitedValue(lhs_val.getBitWidth()));
  947. }
  948. return MakeIntResult(context, lhs.type_id, lhs_is_signed,
  949. std::move(result_val));
  950. }
  951. // Performs a homogeneous builtin binary integer -> integer operation.
  952. static auto PerformBuiltinBinaryIntOp(Context& context, SemIRLoc loc,
  953. SemIR::BuiltinFunctionKind builtin_kind,
  954. SemIR::InstId lhs_id,
  955. SemIR::InstId rhs_id)
  956. -> SemIR::ConstantId {
  957. auto lhs = context.insts().GetAs<SemIR::IntValue>(lhs_id);
  958. auto rhs = context.insts().GetAs<SemIR::IntValue>(rhs_id);
  959. CARBON_CHECK(rhs.type_id == lhs.type_id, "Heterogeneous builtin integer op!");
  960. auto type_id = lhs.type_id;
  961. auto [is_signed, bit_width_id] =
  962. context.sem_ir().types().GetIntTypeInfo(type_id);
  963. auto [lhs_val, rhs_val] =
  964. GetIntsAtSuitableWidth(context, lhs.int_id, rhs.int_id, bit_width_id);
  965. // Check for division by zero.
  966. switch (builtin_kind) {
  967. case SemIR::BuiltinFunctionKind::IntSDiv:
  968. case SemIR::BuiltinFunctionKind::IntSMod:
  969. case SemIR::BuiltinFunctionKind::IntUDiv:
  970. case SemIR::BuiltinFunctionKind::IntUMod:
  971. if (rhs_val.isZero()) {
  972. DiagnoseDivisionByZero(context, loc);
  973. return SemIR::ErrorInst::SingletonConstantId;
  974. }
  975. break;
  976. default:
  977. break;
  978. }
  979. BinaryIntOpResult result =
  980. ComputeBinaryIntOpResult(builtin_kind, lhs_val, rhs_val);
  981. if (result.overflow && !bit_width_id.has_value()) {
  982. // Retry with a larger bit width. Most operations can only overflow by one
  983. // bit, but signed n-bit multiplication can overflow to 2n-1 bits. We don't
  984. // need to handle unsigned multiplication here because it's not permitted
  985. // for unsized integers.
  986. //
  987. // Note that we speculatively first perform the calculation in the width of
  988. // the wider operand: smaller operations are faster and overflow to a wider
  989. // integer is unlikely to be needed, especially given that the width will
  990. // have been rounded up to a multiple of 64 bits by the int store.
  991. CARBON_CHECK(builtin_kind != SemIR::BuiltinFunctionKind::IntUMul,
  992. "Unsigned arithmetic requires a fixed bitwidth");
  993. int new_width =
  994. builtin_kind == SemIR::BuiltinFunctionKind::IntSMul
  995. ? lhs_val.getBitWidth() * 2
  996. : IntStore::CanonicalBitWidth(lhs_val.getBitWidth() + 1);
  997. new_width = std::min(new_width, IntStore::MaxIntWidth);
  998. lhs_val = context.ints().GetAtWidth(lhs.int_id, new_width);
  999. rhs_val = context.ints().GetAtWidth(rhs.int_id, new_width);
  1000. // Note that this can in theory still overflow if we limited `new_width` to
  1001. // `MaxIntWidth`. In that case we fall through to the signed overflow
  1002. // diagnostic below.
  1003. result = ComputeBinaryIntOpResult(builtin_kind, lhs_val, rhs_val);
  1004. CARBON_CHECK(!result.overflow || new_width == IntStore::MaxIntWidth);
  1005. }
  1006. if (result.overflow) {
  1007. CARBON_DIAGNOSTIC(CompileTimeIntegerOverflow, Error,
  1008. "integer overflow in calculation `{0} {1} {2}`", TypedInt,
  1009. Lex::TokenKind, TypedInt);
  1010. context.emitter().Emit(loc, CompileTimeIntegerOverflow,
  1011. {.type = type_id, .value = lhs_val}, result.op_token,
  1012. {.type = type_id, .value = rhs_val});
  1013. }
  1014. return MakeIntResult(context, type_id, is_signed,
  1015. std::move(result.result_val));
  1016. }
  1017. // Performs a builtin integer comparison.
  1018. static auto PerformBuiltinIntComparison(Context& context,
  1019. SemIR::BuiltinFunctionKind builtin_kind,
  1020. SemIR::InstId lhs_id,
  1021. SemIR::InstId rhs_id,
  1022. SemIR::TypeId bool_type_id)
  1023. -> SemIR::ConstantId {
  1024. auto lhs = context.insts().GetAs<SemIR::IntValue>(lhs_id);
  1025. auto rhs = context.insts().GetAs<SemIR::IntValue>(rhs_id);
  1026. llvm::APInt lhs_val = context.ints().Get(lhs.int_id);
  1027. llvm::APInt rhs_val = context.ints().Get(rhs.int_id);
  1028. bool result;
  1029. switch (builtin_kind) {
  1030. case SemIR::BuiltinFunctionKind::IntEq:
  1031. result = (lhs_val == rhs_val);
  1032. break;
  1033. case SemIR::BuiltinFunctionKind::IntNeq:
  1034. result = (lhs_val != rhs_val);
  1035. break;
  1036. case SemIR::BuiltinFunctionKind::IntLess:
  1037. result = lhs_val.slt(rhs_val);
  1038. break;
  1039. case SemIR::BuiltinFunctionKind::IntLessEq:
  1040. result = lhs_val.sle(rhs_val);
  1041. break;
  1042. case SemIR::BuiltinFunctionKind::IntGreater:
  1043. result = lhs_val.sgt(rhs_val);
  1044. break;
  1045. case SemIR::BuiltinFunctionKind::IntGreaterEq:
  1046. result = lhs_val.sge(rhs_val);
  1047. break;
  1048. default:
  1049. CARBON_FATAL("Unexpected operation kind.");
  1050. }
  1051. return MakeBoolResult(context, bool_type_id, result);
  1052. }
  1053. // Performs a builtin unary float -> float operation.
  1054. static auto PerformBuiltinUnaryFloatOp(Context& context,
  1055. SemIR::BuiltinFunctionKind builtin_kind,
  1056. SemIR::InstId arg_id)
  1057. -> SemIR::ConstantId {
  1058. auto op = context.insts().GetAs<SemIR::FloatLiteral>(arg_id);
  1059. auto op_val = context.floats().Get(op.float_id);
  1060. switch (builtin_kind) {
  1061. case SemIR::BuiltinFunctionKind::FloatNegate:
  1062. op_val.changeSign();
  1063. break;
  1064. default:
  1065. CARBON_FATAL("Unexpected builtin kind");
  1066. }
  1067. return MakeFloatResult(context, op.type_id, std::move(op_val));
  1068. }
  1069. // Performs a builtin binary float -> float operation.
  1070. static auto PerformBuiltinBinaryFloatOp(Context& context,
  1071. SemIR::BuiltinFunctionKind builtin_kind,
  1072. SemIR::InstId lhs_id,
  1073. SemIR::InstId rhs_id)
  1074. -> SemIR::ConstantId {
  1075. auto lhs = context.insts().GetAs<SemIR::FloatLiteral>(lhs_id);
  1076. auto rhs = context.insts().GetAs<SemIR::FloatLiteral>(rhs_id);
  1077. auto lhs_val = context.floats().Get(lhs.float_id);
  1078. auto rhs_val = context.floats().Get(rhs.float_id);
  1079. llvm::APFloat result_val(lhs_val.getSemantics());
  1080. switch (builtin_kind) {
  1081. case SemIR::BuiltinFunctionKind::FloatAdd:
  1082. result_val = lhs_val + rhs_val;
  1083. break;
  1084. case SemIR::BuiltinFunctionKind::FloatSub:
  1085. result_val = lhs_val - rhs_val;
  1086. break;
  1087. case SemIR::BuiltinFunctionKind::FloatMul:
  1088. result_val = lhs_val * rhs_val;
  1089. break;
  1090. case SemIR::BuiltinFunctionKind::FloatDiv:
  1091. result_val = lhs_val / rhs_val;
  1092. break;
  1093. default:
  1094. CARBON_FATAL("Unexpected operation kind.");
  1095. }
  1096. return MakeFloatResult(context, lhs.type_id, std::move(result_val));
  1097. }
  1098. // Performs a builtin float comparison.
  1099. static auto PerformBuiltinFloatComparison(
  1100. Context& context, SemIR::BuiltinFunctionKind builtin_kind,
  1101. SemIR::InstId lhs_id, SemIR::InstId rhs_id, SemIR::TypeId bool_type_id)
  1102. -> SemIR::ConstantId {
  1103. auto lhs = context.insts().GetAs<SemIR::FloatLiteral>(lhs_id);
  1104. auto rhs = context.insts().GetAs<SemIR::FloatLiteral>(rhs_id);
  1105. const auto& lhs_val = context.floats().Get(lhs.float_id);
  1106. const auto& rhs_val = context.floats().Get(rhs.float_id);
  1107. bool result;
  1108. switch (builtin_kind) {
  1109. case SemIR::BuiltinFunctionKind::FloatEq:
  1110. result = (lhs_val == rhs_val);
  1111. break;
  1112. case SemIR::BuiltinFunctionKind::FloatNeq:
  1113. result = (lhs_val != rhs_val);
  1114. break;
  1115. case SemIR::BuiltinFunctionKind::FloatLess:
  1116. result = lhs_val < rhs_val;
  1117. break;
  1118. case SemIR::BuiltinFunctionKind::FloatLessEq:
  1119. result = lhs_val <= rhs_val;
  1120. break;
  1121. case SemIR::BuiltinFunctionKind::FloatGreater:
  1122. result = lhs_val > rhs_val;
  1123. break;
  1124. case SemIR::BuiltinFunctionKind::FloatGreaterEq:
  1125. result = lhs_val >= rhs_val;
  1126. break;
  1127. default:
  1128. CARBON_FATAL("Unexpected operation kind.");
  1129. }
  1130. return MakeBoolResult(context, bool_type_id, result);
  1131. }
  1132. // Performs a builtin boolean comparison.
  1133. static auto PerformBuiltinBoolComparison(
  1134. Context& context, SemIR::BuiltinFunctionKind builtin_kind,
  1135. SemIR::InstId lhs_id, SemIR::InstId rhs_id, SemIR::TypeId bool_type_id) {
  1136. bool lhs = context.insts().GetAs<SemIR::BoolLiteral>(lhs_id).value.ToBool();
  1137. bool rhs = context.insts().GetAs<SemIR::BoolLiteral>(rhs_id).value.ToBool();
  1138. return MakeBoolResult(context, bool_type_id,
  1139. builtin_kind == SemIR::BuiltinFunctionKind::BoolEq
  1140. ? lhs == rhs
  1141. : lhs != rhs);
  1142. }
  1143. // Returns a constant for a call to a builtin function.
  1144. static auto MakeConstantForBuiltinCall(Context& context, SemIRLoc loc,
  1145. SemIR::Call call,
  1146. SemIR::BuiltinFunctionKind builtin_kind,
  1147. llvm::ArrayRef<SemIR::InstId> arg_ids,
  1148. Phase phase) -> SemIR::ConstantId {
  1149. switch (builtin_kind) {
  1150. case SemIR::BuiltinFunctionKind::None:
  1151. CARBON_FATAL("Not a builtin function.");
  1152. case SemIR::BuiltinFunctionKind::PrintChar:
  1153. case SemIR::BuiltinFunctionKind::PrintInt:
  1154. case SemIR::BuiltinFunctionKind::ReadChar: {
  1155. // These are runtime-only builtins.
  1156. // TODO: Consider tracking this on the `BuiltinFunctionKind`.
  1157. return SemIR::ConstantId::NotConstant;
  1158. }
  1159. case SemIR::BuiltinFunctionKind::IntLiteralMakeType: {
  1160. return context.constant_values().Get(
  1161. SemIR::IntLiteralType::SingletonInstId);
  1162. }
  1163. case SemIR::BuiltinFunctionKind::IntMakeTypeSigned: {
  1164. return MakeIntTypeResult(context, loc, SemIR::IntKind::Signed, arg_ids[0],
  1165. phase);
  1166. }
  1167. case SemIR::BuiltinFunctionKind::IntMakeTypeUnsigned: {
  1168. return MakeIntTypeResult(context, loc, SemIR::IntKind::Unsigned,
  1169. arg_ids[0], phase);
  1170. }
  1171. case SemIR::BuiltinFunctionKind::FloatMakeType: {
  1172. // TODO: Support a symbolic constant width.
  1173. if (phase != Phase::Template) {
  1174. break;
  1175. }
  1176. if (!ValidateFloatBitWidth(context, loc, arg_ids[0])) {
  1177. return SemIR::ErrorInst::SingletonConstantId;
  1178. }
  1179. return context.constant_values().Get(
  1180. SemIR::LegacyFloatType::SingletonInstId);
  1181. }
  1182. case SemIR::BuiltinFunctionKind::BoolMakeType: {
  1183. return context.constant_values().Get(SemIR::BoolType::SingletonInstId);
  1184. }
  1185. // Integer conversions.
  1186. case SemIR::BuiltinFunctionKind::IntConvert: {
  1187. if (phase == Phase::Symbolic) {
  1188. return MakeConstantResult(context, call, phase);
  1189. }
  1190. return PerformIntConvert(context, arg_ids[0], call.type_id);
  1191. }
  1192. case SemIR::BuiltinFunctionKind::IntConvertChecked: {
  1193. if (phase == Phase::Symbolic) {
  1194. return MakeConstantResult(context, call, phase);
  1195. }
  1196. return PerformCheckedIntConvert(context, loc, arg_ids[0], call.type_id);
  1197. }
  1198. // Unary integer -> integer operations.
  1199. case SemIR::BuiltinFunctionKind::IntSNegate:
  1200. case SemIR::BuiltinFunctionKind::IntUNegate:
  1201. case SemIR::BuiltinFunctionKind::IntComplement: {
  1202. if (phase != Phase::Template) {
  1203. break;
  1204. }
  1205. return PerformBuiltinUnaryIntOp(context, loc, builtin_kind, arg_ids[0]);
  1206. }
  1207. // Homogeneous binary integer -> integer operations.
  1208. case SemIR::BuiltinFunctionKind::IntSAdd:
  1209. case SemIR::BuiltinFunctionKind::IntSSub:
  1210. case SemIR::BuiltinFunctionKind::IntSMul:
  1211. case SemIR::BuiltinFunctionKind::IntSDiv:
  1212. case SemIR::BuiltinFunctionKind::IntSMod:
  1213. case SemIR::BuiltinFunctionKind::IntUAdd:
  1214. case SemIR::BuiltinFunctionKind::IntUSub:
  1215. case SemIR::BuiltinFunctionKind::IntUMul:
  1216. case SemIR::BuiltinFunctionKind::IntUDiv:
  1217. case SemIR::BuiltinFunctionKind::IntUMod:
  1218. case SemIR::BuiltinFunctionKind::IntAnd:
  1219. case SemIR::BuiltinFunctionKind::IntOr:
  1220. case SemIR::BuiltinFunctionKind::IntXor: {
  1221. if (phase != Phase::Template) {
  1222. break;
  1223. }
  1224. return PerformBuiltinBinaryIntOp(context, loc, builtin_kind, arg_ids[0],
  1225. arg_ids[1]);
  1226. }
  1227. // Bit shift operations.
  1228. case SemIR::BuiltinFunctionKind::IntLeftShift:
  1229. case SemIR::BuiltinFunctionKind::IntRightShift: {
  1230. if (phase != Phase::Template) {
  1231. break;
  1232. }
  1233. return PerformBuiltinIntShiftOp(context, loc, builtin_kind, arg_ids[0],
  1234. arg_ids[1]);
  1235. }
  1236. // Integer comparisons.
  1237. case SemIR::BuiltinFunctionKind::IntEq:
  1238. case SemIR::BuiltinFunctionKind::IntNeq:
  1239. case SemIR::BuiltinFunctionKind::IntLess:
  1240. case SemIR::BuiltinFunctionKind::IntLessEq:
  1241. case SemIR::BuiltinFunctionKind::IntGreater:
  1242. case SemIR::BuiltinFunctionKind::IntGreaterEq: {
  1243. if (phase != Phase::Template) {
  1244. break;
  1245. }
  1246. return PerformBuiltinIntComparison(context, builtin_kind, arg_ids[0],
  1247. arg_ids[1], call.type_id);
  1248. }
  1249. // Unary float -> float operations.
  1250. case SemIR::BuiltinFunctionKind::FloatNegate: {
  1251. if (phase != Phase::Template) {
  1252. break;
  1253. }
  1254. return PerformBuiltinUnaryFloatOp(context, builtin_kind, arg_ids[0]);
  1255. }
  1256. // Binary float -> float operations.
  1257. case SemIR::BuiltinFunctionKind::FloatAdd:
  1258. case SemIR::BuiltinFunctionKind::FloatSub:
  1259. case SemIR::BuiltinFunctionKind::FloatMul:
  1260. case SemIR::BuiltinFunctionKind::FloatDiv: {
  1261. if (phase != Phase::Template) {
  1262. break;
  1263. }
  1264. return PerformBuiltinBinaryFloatOp(context, builtin_kind, arg_ids[0],
  1265. arg_ids[1]);
  1266. }
  1267. // Float comparisons.
  1268. case SemIR::BuiltinFunctionKind::FloatEq:
  1269. case SemIR::BuiltinFunctionKind::FloatNeq:
  1270. case SemIR::BuiltinFunctionKind::FloatLess:
  1271. case SemIR::BuiltinFunctionKind::FloatLessEq:
  1272. case SemIR::BuiltinFunctionKind::FloatGreater:
  1273. case SemIR::BuiltinFunctionKind::FloatGreaterEq: {
  1274. if (phase != Phase::Template) {
  1275. break;
  1276. }
  1277. return PerformBuiltinFloatComparison(context, builtin_kind, arg_ids[0],
  1278. arg_ids[1], call.type_id);
  1279. }
  1280. // Bool comparisons.
  1281. case SemIR::BuiltinFunctionKind::BoolEq:
  1282. case SemIR::BuiltinFunctionKind::BoolNeq: {
  1283. if (phase != Phase::Template) {
  1284. break;
  1285. }
  1286. return PerformBuiltinBoolComparison(context, builtin_kind, arg_ids[0],
  1287. arg_ids[1], call.type_id);
  1288. }
  1289. }
  1290. return SemIR::ConstantId::NotConstant;
  1291. }
  1292. // Makes a constant for a call instruction.
  1293. static auto MakeConstantForCall(EvalContext& eval_context, SemIRLoc loc,
  1294. SemIR::Call call) -> SemIR::ConstantId {
  1295. Phase phase = Phase::Template;
  1296. // A call with an invalid argument list is used to represent an erroneous
  1297. // call.
  1298. //
  1299. // TODO: Use a better representation for this.
  1300. if (call.args_id == SemIR::InstBlockId::None) {
  1301. return SemIR::ErrorInst::SingletonConstantId;
  1302. }
  1303. // Find the constant value of the callee.
  1304. bool has_constant_callee = ReplaceFieldWithConstantValue(
  1305. eval_context, &call, &SemIR::Call::callee_id, &phase);
  1306. auto callee_function =
  1307. SemIR::GetCalleeFunction(eval_context.sem_ir(), call.callee_id);
  1308. auto builtin_kind = SemIR::BuiltinFunctionKind::None;
  1309. if (callee_function.function_id.has_value()) {
  1310. // Calls to builtins might be constant.
  1311. builtin_kind = eval_context.functions()
  1312. .Get(callee_function.function_id)
  1313. .builtin_function_kind;
  1314. if (builtin_kind == SemIR::BuiltinFunctionKind::None) {
  1315. // TODO: Eventually we'll want to treat some kinds of non-builtin
  1316. // functions as producing constants.
  1317. return SemIR::ConstantId::NotConstant;
  1318. }
  1319. } else {
  1320. // Calls to non-functions, such as calls to generic entity names, might be
  1321. // constant.
  1322. }
  1323. // Find the argument values and the return type.
  1324. bool has_constant_operands =
  1325. has_constant_callee &&
  1326. ReplaceFieldWithConstantValue(eval_context, &call, &SemIR::Call::type_id,
  1327. &phase) &&
  1328. ReplaceFieldWithConstantValue(eval_context, &call, &SemIR::Call::args_id,
  1329. &phase);
  1330. if (phase == Phase::UnknownDueToError) {
  1331. return SemIR::ErrorInst::SingletonConstantId;
  1332. }
  1333. // If any operand of the call is non-constant, the call is non-constant.
  1334. // TODO: Some builtin calls might allow some operands to be non-constant.
  1335. if (!has_constant_operands) {
  1336. if (builtin_kind.IsCompTimeOnly(
  1337. eval_context.sem_ir(), eval_context.inst_blocks().Get(call.args_id),
  1338. call.type_id)) {
  1339. CARBON_DIAGNOSTIC(NonConstantCallToCompTimeOnlyFunction, Error,
  1340. "non-constant call to compile-time-only function");
  1341. CARBON_DIAGNOSTIC(CompTimeOnlyFunctionHere, Note,
  1342. "compile-time-only function declared here");
  1343. eval_context.emitter()
  1344. .Build(loc, NonConstantCallToCompTimeOnlyFunction)
  1345. .Note(eval_context.functions()
  1346. .Get(callee_function.function_id)
  1347. .latest_decl_id(),
  1348. CompTimeOnlyFunctionHere)
  1349. .Emit();
  1350. }
  1351. return SemIR::ConstantId::NotConstant;
  1352. }
  1353. // Handle calls to builtins.
  1354. if (builtin_kind != SemIR::BuiltinFunctionKind::None) {
  1355. return MakeConstantForBuiltinCall(
  1356. eval_context.context(), loc, call, builtin_kind,
  1357. eval_context.inst_blocks().Get(call.args_id), phase);
  1358. }
  1359. return SemIR::ConstantId::NotConstant;
  1360. }
  1361. // Creates a FacetType constant.
  1362. static auto MakeFacetTypeResult(Context& context,
  1363. const SemIR::FacetTypeInfo& info, Phase phase)
  1364. -> SemIR::ConstantId {
  1365. SemIR::FacetTypeId facet_type_id = context.facet_types().Add(info);
  1366. return MakeConstantResult(
  1367. context,
  1368. SemIR::FacetType{.type_id = SemIR::TypeType::SingletonTypeId,
  1369. .facet_type_id = facet_type_id},
  1370. phase);
  1371. }
  1372. // Implementation for `TryEvalInst`, wrapping `Context` with `EvalContext`.
  1373. static auto TryEvalInstInContext(EvalContext& eval_context,
  1374. SemIR::InstId inst_id, SemIR::Inst inst)
  1375. -> SemIR::ConstantId {
  1376. // TODO: Ensure we have test coverage for each of these cases that can result
  1377. // in a constant, once those situations are all reachable.
  1378. CARBON_KIND_SWITCH(inst) {
  1379. // These cases are constants if their operands are.
  1380. case SemIR::AddrOf::Kind:
  1381. return RebuildIfFieldsAreConstant(eval_context, inst,
  1382. &SemIR::AddrOf::type_id,
  1383. &SemIR::AddrOf::lvalue_id);
  1384. case CARBON_KIND(SemIR::ArrayType array_type): {
  1385. return RebuildAndValidateIfFieldsAreConstant(
  1386. eval_context, inst,
  1387. [&](SemIR::ArrayType result) {
  1388. auto bound_id = array_type.bound_id;
  1389. auto bound_inst = eval_context.insts().Get(result.bound_id);
  1390. auto int_bound = bound_inst.TryAs<SemIR::IntValue>();
  1391. if (!int_bound) {
  1392. CARBON_CHECK(eval_context.constant_values()
  1393. .Get(result.bound_id)
  1394. .is_symbolic(),
  1395. "Unexpected inst {0} for template constant int",
  1396. bound_inst);
  1397. return true;
  1398. }
  1399. // TODO: We should check that the size of the resulting array type
  1400. // fits in 64 bits, not just that the bound does. Should we use a
  1401. // 32-bit limit for 32-bit targets?
  1402. const auto& bound_val = eval_context.ints().Get(int_bound->int_id);
  1403. if (eval_context.types().IsSignedInt(int_bound->type_id) &&
  1404. bound_val.isNegative()) {
  1405. CARBON_DIAGNOSTIC(ArrayBoundNegative, Error,
  1406. "array bound of {0} is negative", TypedInt);
  1407. eval_context.emitter().Emit(
  1408. eval_context.GetDiagnosticLoc(bound_id), ArrayBoundNegative,
  1409. {.type = int_bound->type_id, .value = bound_val});
  1410. return false;
  1411. }
  1412. if (bound_val.getActiveBits() > 64) {
  1413. CARBON_DIAGNOSTIC(ArrayBoundTooLarge, Error,
  1414. "array bound of {0} is too large", TypedInt);
  1415. eval_context.emitter().Emit(
  1416. eval_context.GetDiagnosticLoc(bound_id), ArrayBoundTooLarge,
  1417. {.type = int_bound->type_id, .value = bound_val});
  1418. return false;
  1419. }
  1420. return true;
  1421. },
  1422. &SemIR::ArrayType::bound_id, &SemIR::ArrayType::element_type_id);
  1423. }
  1424. case SemIR::AssociatedEntity::Kind:
  1425. return RebuildIfFieldsAreConstant(eval_context, inst,
  1426. &SemIR::AssociatedEntity::type_id);
  1427. case SemIR::AssociatedEntityType::Kind:
  1428. return RebuildIfFieldsAreConstant(
  1429. eval_context, inst, &SemIR::AssociatedEntityType::interface_type_id);
  1430. case SemIR::BoundMethod::Kind:
  1431. return RebuildIfFieldsAreConstant(eval_context, inst,
  1432. &SemIR::BoundMethod::type_id,
  1433. &SemIR::BoundMethod::object_id,
  1434. &SemIR::BoundMethod::function_decl_id);
  1435. case SemIR::ClassType::Kind:
  1436. return RebuildIfFieldsAreConstant(eval_context, inst,
  1437. &SemIR::ClassType::specific_id);
  1438. case SemIR::CompleteTypeWitness::Kind:
  1439. return RebuildIfFieldsAreConstant(
  1440. eval_context, inst, &SemIR::CompleteTypeWitness::object_repr_id);
  1441. case SemIR::FacetValue::Kind:
  1442. return RebuildIfFieldsAreConstant(eval_context, inst,
  1443. &SemIR::FacetValue::type_id,
  1444. &SemIR::FacetValue::type_inst_id,
  1445. &SemIR::FacetValue::witness_inst_id);
  1446. case SemIR::FunctionType::Kind:
  1447. return RebuildIfFieldsAreConstant(eval_context, inst,
  1448. &SemIR::FunctionType::specific_id);
  1449. case SemIR::FunctionTypeWithSelfType::Kind:
  1450. return RebuildIfFieldsAreConstant(
  1451. eval_context, inst,
  1452. &SemIR::FunctionTypeWithSelfType::interface_function_type_id,
  1453. &SemIR::FunctionTypeWithSelfType::self_id);
  1454. case SemIR::GenericClassType::Kind:
  1455. return RebuildIfFieldsAreConstant(
  1456. eval_context, inst, &SemIR::GenericClassType::enclosing_specific_id);
  1457. case SemIR::GenericInterfaceType::Kind:
  1458. return RebuildIfFieldsAreConstant(
  1459. eval_context, inst,
  1460. &SemIR::GenericInterfaceType::enclosing_specific_id);
  1461. case SemIR::ImplWitness::Kind:
  1462. // We intentionally don't replace the `elements_id` field here. We want to
  1463. // track that specific InstBlock in particular, not coalesce blocks with
  1464. // the same members. That block may get updated, and we want to pick up
  1465. // those changes.
  1466. return RebuildIfFieldsAreConstant(eval_context, inst,
  1467. &SemIR::ImplWitness::specific_id);
  1468. case CARBON_KIND(SemIR::IntType int_type): {
  1469. return RebuildAndValidateIfFieldsAreConstant(
  1470. eval_context, inst,
  1471. [&](SemIR::IntType result) {
  1472. return ValidateIntType(
  1473. eval_context.context(),
  1474. eval_context.GetDiagnosticLoc({inst_id, int_type.bit_width_id}),
  1475. result);
  1476. },
  1477. &SemIR::IntType::bit_width_id);
  1478. }
  1479. case SemIR::PointerType::Kind:
  1480. return RebuildIfFieldsAreConstant(eval_context, inst,
  1481. &SemIR::PointerType::pointee_id);
  1482. case CARBON_KIND(SemIR::FloatType float_type): {
  1483. return RebuildAndValidateIfFieldsAreConstant(
  1484. eval_context, inst,
  1485. [&](SemIR::FloatType result) {
  1486. return ValidateFloatType(eval_context.context(),
  1487. eval_context.GetDiagnosticLoc(
  1488. {inst_id, float_type.bit_width_id}),
  1489. result);
  1490. },
  1491. &SemIR::FloatType::bit_width_id);
  1492. }
  1493. case SemIR::SpecificFunction::Kind:
  1494. return RebuildIfFieldsAreConstant(eval_context, inst,
  1495. &SemIR::SpecificFunction::callee_id,
  1496. &SemIR::SpecificFunction::specific_id);
  1497. case SemIR::StructType::Kind:
  1498. return RebuildIfFieldsAreConstant(eval_context, inst,
  1499. &SemIR::StructType::fields_id);
  1500. case SemIR::StructValue::Kind:
  1501. return RebuildIfFieldsAreConstant(eval_context, inst,
  1502. &SemIR::StructValue::type_id,
  1503. &SemIR::StructValue::elements_id);
  1504. case SemIR::TupleType::Kind:
  1505. return RebuildIfFieldsAreConstant(eval_context, inst,
  1506. &SemIR::TupleType::elements_id);
  1507. case SemIR::TupleValue::Kind:
  1508. return RebuildIfFieldsAreConstant(eval_context, inst,
  1509. &SemIR::TupleValue::type_id,
  1510. &SemIR::TupleValue::elements_id);
  1511. case SemIR::UnboundElementType::Kind:
  1512. return RebuildIfFieldsAreConstant(
  1513. eval_context, inst, &SemIR::UnboundElementType::class_type_id,
  1514. &SemIR::UnboundElementType::element_type_id);
  1515. // Initializers evaluate to a value of the object representation.
  1516. case SemIR::ArrayInit::Kind:
  1517. // TODO: Add an `ArrayValue` to represent a constant array object
  1518. // representation instead of using a `TupleValue`.
  1519. return RebuildInitAsValue(eval_context, inst, SemIR::TupleValue::Kind);
  1520. case SemIR::ClassInit::Kind:
  1521. // TODO: Add a `ClassValue` to represent a constant class object
  1522. // representation instead of using a `StructValue`.
  1523. return RebuildInitAsValue(eval_context, inst, SemIR::StructValue::Kind);
  1524. case SemIR::StructInit::Kind:
  1525. return RebuildInitAsValue(eval_context, inst, SemIR::StructValue::Kind);
  1526. case SemIR::TupleInit::Kind:
  1527. return RebuildInitAsValue(eval_context, inst, SemIR::TupleValue::Kind);
  1528. case SemIR::Vtable::Kind:
  1529. return RebuildIfFieldsAreConstant(eval_context, inst,
  1530. &SemIR::Vtable::virtual_functions_id);
  1531. case SemIR::AutoType::Kind:
  1532. case SemIR::BoolType::Kind:
  1533. case SemIR::BoundMethodType::Kind:
  1534. case SemIR::ErrorInst::Kind:
  1535. case SemIR::IntLiteralType::Kind:
  1536. case SemIR::LegacyFloatType::Kind:
  1537. case SemIR::NamespaceType::Kind:
  1538. case SemIR::SpecificFunctionType::Kind:
  1539. case SemIR::StringType::Kind:
  1540. case SemIR::TypeType::Kind:
  1541. case SemIR::VtableType::Kind:
  1542. case SemIR::WitnessType::Kind:
  1543. // Builtins are always template constants.
  1544. return MakeConstantResult(eval_context.context(), inst, Phase::Template);
  1545. case CARBON_KIND(SemIR::FunctionDecl fn_decl): {
  1546. return TransformIfFieldsAreConstant(
  1547. eval_context, fn_decl,
  1548. [&](SemIR::FunctionDecl result) {
  1549. return SemIR::StructValue{.type_id = result.type_id,
  1550. .elements_id = SemIR::InstBlockId::Empty};
  1551. },
  1552. &SemIR::FunctionDecl::type_id);
  1553. }
  1554. case CARBON_KIND(SemIR::ClassDecl class_decl): {
  1555. // If the class has generic parameters, we don't produce a class type, but
  1556. // a callable whose return value is a class type.
  1557. if (eval_context.classes().Get(class_decl.class_id).has_parameters()) {
  1558. return TransformIfFieldsAreConstant(
  1559. eval_context, class_decl,
  1560. [&](SemIR::ClassDecl result) {
  1561. return SemIR::StructValue{
  1562. .type_id = result.type_id,
  1563. .elements_id = SemIR::InstBlockId::Empty};
  1564. },
  1565. &SemIR::ClassDecl::type_id);
  1566. }
  1567. // A non-generic class declaration evaluates to the class type.
  1568. return MakeConstantResult(
  1569. eval_context.context(),
  1570. SemIR::ClassType{.type_id = SemIR::TypeType::SingletonTypeId,
  1571. .class_id = class_decl.class_id,
  1572. .specific_id = SemIR::SpecificId::None},
  1573. Phase::Template);
  1574. }
  1575. case CARBON_KIND(SemIR::FacetType facet_type): {
  1576. Phase phase = Phase::Template;
  1577. SemIR::FacetTypeInfo info = GetConstantFacetTypeInfo(
  1578. eval_context, facet_type.facet_type_id, &phase);
  1579. info.Canonicalize();
  1580. // TODO: Reuse `inst` if we can detect that nothing has changed.
  1581. return MakeFacetTypeResult(eval_context.context(), info, phase);
  1582. }
  1583. case CARBON_KIND(SemIR::InterfaceDecl interface_decl): {
  1584. // If the interface has generic parameters, we don't produce an interface
  1585. // type, but a callable whose return value is an interface type.
  1586. if (eval_context.interfaces()
  1587. .Get(interface_decl.interface_id)
  1588. .has_parameters()) {
  1589. return TransformIfFieldsAreConstant(
  1590. eval_context, interface_decl,
  1591. [&](SemIR::InterfaceDecl result) {
  1592. return SemIR::StructValue{
  1593. .type_id = result.type_id,
  1594. .elements_id = SemIR::InstBlockId::Empty};
  1595. },
  1596. &SemIR::InterfaceDecl::type_id);
  1597. }
  1598. // A non-generic interface declaration evaluates to a facet type.
  1599. return MakeConstantResult(
  1600. eval_context.context(),
  1601. eval_context.context().FacetTypeFromInterface(
  1602. interface_decl.interface_id, SemIR::SpecificId::None),
  1603. Phase::Template);
  1604. }
  1605. case CARBON_KIND(SemIR::SpecificConstant specific): {
  1606. // Pull the constant value out of the specific.
  1607. return SemIR::GetConstantValueInSpecific(
  1608. eval_context.sem_ir(), specific.specific_id, specific.inst_id);
  1609. }
  1610. // These cases are treated as being the unique canonical definition of the
  1611. // corresponding constant value.
  1612. // TODO: This doesn't properly handle redeclarations. Consider adding a
  1613. // corresponding `Value` inst for each of these cases, or returning the
  1614. // first declaration.
  1615. case SemIR::AdaptDecl::Kind:
  1616. case SemIR::AssociatedConstantDecl::Kind:
  1617. case SemIR::BaseDecl::Kind:
  1618. case SemIR::FieldDecl::Kind:
  1619. case SemIR::ImplDecl::Kind:
  1620. case SemIR::Namespace::Kind:
  1621. return SemIR::ConstantId::ForTemplateConstant(inst_id);
  1622. case SemIR::BoolLiteral::Kind:
  1623. case SemIR::FloatLiteral::Kind:
  1624. case SemIR::IntValue::Kind:
  1625. case SemIR::StringLiteral::Kind:
  1626. // Promote literals to the constant block.
  1627. // TODO: Convert literals into a canonical form. Currently we can form two
  1628. // different `i32` constants with the same value if they are represented
  1629. // by `APInt`s with different bit widths.
  1630. // TODO: Can the type of an IntValue or FloatLiteral be symbolic? If so,
  1631. // we may need to rebuild.
  1632. return MakeConstantResult(eval_context.context(), inst, Phase::Template);
  1633. // The elements of a constant aggregate can be accessed.
  1634. case SemIR::ClassElementAccess::Kind:
  1635. case SemIR::StructAccess::Kind:
  1636. case SemIR::TupleAccess::Kind:
  1637. return PerformAggregateAccess(eval_context, inst);
  1638. case CARBON_KIND(SemIR::ImplWitnessAccess access_inst): {
  1639. // This is PerformAggregateAccess followed by GetConstantInSpecific.
  1640. Phase phase = Phase::Template;
  1641. if (ReplaceFieldWithConstantValue(eval_context, &access_inst,
  1642. &SemIR::ImplWitnessAccess::witness_id,
  1643. &phase)) {
  1644. if (auto witness = eval_context.insts().TryGetAs<SemIR::ImplWitness>(
  1645. access_inst.witness_id)) {
  1646. auto elements = eval_context.inst_blocks().Get(witness->elements_id);
  1647. auto index = static_cast<size_t>(access_inst.index.index);
  1648. CARBON_CHECK(index < elements.size(), "Access out of bounds.");
  1649. // `Phase` is not used here. If this element is a template constant,
  1650. // then so is the result of indexing, even if the aggregate also
  1651. // contains a symbolic context.
  1652. auto element = elements[index];
  1653. if (!element.has_value()) {
  1654. // TODO: Perhaps this should be a `{}` value with incomplete type?
  1655. CARBON_DIAGNOSTIC(ImplAccessMemberBeforeComplete, Error,
  1656. "accessing member from impl before the end of "
  1657. "its definition");
  1658. // TODO: Add note pointing to the impl declaration.
  1659. eval_context.emitter().Emit(eval_context.GetDiagnosticLoc(inst_id),
  1660. ImplAccessMemberBeforeComplete);
  1661. return SemIR::ErrorInst::SingletonConstantId;
  1662. }
  1663. LoadImportRef(eval_context.context(), element);
  1664. return GetConstantValueInSpecific(eval_context.sem_ir(),
  1665. witness->specific_id, element);
  1666. } else {
  1667. CARBON_CHECK(phase != Phase::Template,
  1668. "Failed to evaluate template constant {0} arg0: {1}",
  1669. inst, eval_context.insts().Get(access_inst.witness_id));
  1670. }
  1671. return MakeConstantResult(eval_context.context(), access_inst, phase);
  1672. }
  1673. return MakeNonConstantResult(phase);
  1674. }
  1675. case CARBON_KIND(SemIR::ArrayIndex index): {
  1676. return PerformArrayIndex(eval_context, index);
  1677. }
  1678. case CARBON_KIND(SemIR::Call call): {
  1679. return MakeConstantForCall(eval_context,
  1680. eval_context.GetDiagnosticLoc(inst_id), call);
  1681. }
  1682. // TODO: These need special handling.
  1683. case SemIR::BindValue::Kind:
  1684. case SemIR::Deref::Kind:
  1685. case SemIR::ImportRefLoaded::Kind:
  1686. case SemIR::ReturnSlot::Kind:
  1687. case SemIR::Temporary::Kind:
  1688. case SemIR::TemporaryStorage::Kind:
  1689. case SemIR::ValueAsRef::Kind:
  1690. case SemIR::VtablePtr::Kind:
  1691. break;
  1692. case CARBON_KIND(SemIR::SymbolicBindingPattern bind): {
  1693. // TODO: Disable constant evaluation of SymbolicBindingPattern once
  1694. // DeduceGenericCallArguments no longer needs implicit params to have
  1695. // constant values.
  1696. const auto& bind_name =
  1697. eval_context.entity_names().Get(bind.entity_name_id);
  1698. // If we know which specific we're evaluating within and this is an
  1699. // argument of that specific, its constant value is the corresponding
  1700. // argument value.
  1701. if (auto value =
  1702. eval_context.GetCompileTimeBindValue(bind_name.bind_index);
  1703. value.has_value()) {
  1704. return value;
  1705. }
  1706. // The constant form of a symbolic binding is an idealized form of the
  1707. // original, with no equivalent value.
  1708. bind.entity_name_id =
  1709. eval_context.entity_names().MakeCanonical(bind.entity_name_id);
  1710. return MakeConstantResult(eval_context.context(), bind, Phase::Symbolic);
  1711. }
  1712. case CARBON_KIND(SemIR::BindSymbolicName bind): {
  1713. const auto& bind_name =
  1714. eval_context.entity_names().Get(bind.entity_name_id);
  1715. Phase phase;
  1716. if (bind_name.name_id == SemIR::NameId::PeriodSelf) {
  1717. phase = Phase::PeriodSelfSymbolic;
  1718. } else {
  1719. // If we know which specific we're evaluating within and this is an
  1720. // argument of that specific, its constant value is the corresponding
  1721. // argument value.
  1722. if (auto value =
  1723. eval_context.GetCompileTimeBindValue(bind_name.bind_index);
  1724. value.has_value()) {
  1725. return value;
  1726. }
  1727. phase = Phase::Symbolic;
  1728. }
  1729. // The constant form of a symbolic binding is an idealized form of the
  1730. // original, with no equivalent value.
  1731. bind.entity_name_id =
  1732. eval_context.entity_names().MakeCanonical(bind.entity_name_id);
  1733. bind.value_id = SemIR::InstId::None;
  1734. if (!ReplaceFieldWithConstantValue(
  1735. eval_context, &bind, &SemIR::BindSymbolicName::type_id, &phase)) {
  1736. return MakeNonConstantResult(phase);
  1737. }
  1738. return MakeConstantResult(eval_context.context(), bind, phase);
  1739. }
  1740. // These semantic wrappers don't change the constant value.
  1741. case CARBON_KIND(SemIR::AsCompatible inst): {
  1742. return eval_context.GetConstantValue(inst.source_id);
  1743. }
  1744. case CARBON_KIND(SemIR::BindAlias typed_inst): {
  1745. return eval_context.GetConstantValue(typed_inst.value_id);
  1746. }
  1747. case CARBON_KIND(SemIR::ExportDecl typed_inst): {
  1748. return eval_context.GetConstantValue(typed_inst.value_id);
  1749. }
  1750. case CARBON_KIND(SemIR::NameRef typed_inst): {
  1751. return eval_context.GetConstantValue(typed_inst.value_id);
  1752. }
  1753. case CARBON_KIND(SemIR::ValueParamPattern param_pattern): {
  1754. // TODO: Treat this as a non-expression (here and in GetExprCategory)
  1755. // once generic deduction doesn't need patterns to have constant values.
  1756. return eval_context.GetConstantValue(param_pattern.subpattern_id);
  1757. }
  1758. case CARBON_KIND(SemIR::Converted typed_inst): {
  1759. return eval_context.GetConstantValue(typed_inst.result_id);
  1760. }
  1761. case CARBON_KIND(SemIR::InitializeFrom typed_inst): {
  1762. return eval_context.GetConstantValue(typed_inst.src_id);
  1763. }
  1764. case CARBON_KIND(SemIR::SpliceBlock typed_inst): {
  1765. return eval_context.GetConstantValue(typed_inst.result_id);
  1766. }
  1767. case CARBON_KIND(SemIR::ValueOfInitializer typed_inst): {
  1768. return eval_context.GetConstantValue(typed_inst.init_id);
  1769. }
  1770. case CARBON_KIND(SemIR::FacetAccessType typed_inst): {
  1771. Phase phase = Phase::Template;
  1772. if (ReplaceFieldWithConstantValue(
  1773. eval_context, &typed_inst,
  1774. &SemIR::FacetAccessType::facet_value_inst_id, &phase)) {
  1775. if (auto facet_value = eval_context.insts().TryGetAs<SemIR::FacetValue>(
  1776. typed_inst.facet_value_inst_id)) {
  1777. return eval_context.constant_values().Get(facet_value->type_inst_id);
  1778. }
  1779. return MakeConstantResult(eval_context.context(), typed_inst, phase);
  1780. } else {
  1781. return MakeNonConstantResult(phase);
  1782. }
  1783. }
  1784. case CARBON_KIND(SemIR::FacetAccessWitness typed_inst): {
  1785. Phase phase = Phase::Template;
  1786. if (ReplaceFieldWithConstantValue(
  1787. eval_context, &typed_inst,
  1788. &SemIR::FacetAccessWitness::facet_value_inst_id, &phase)) {
  1789. if (auto facet_value = eval_context.insts().TryGetAs<SemIR::FacetValue>(
  1790. typed_inst.facet_value_inst_id)) {
  1791. return eval_context.constant_values().Get(
  1792. facet_value->witness_inst_id);
  1793. }
  1794. return MakeConstantResult(eval_context.context(), typed_inst, phase);
  1795. } else {
  1796. return MakeNonConstantResult(phase);
  1797. }
  1798. }
  1799. case CARBON_KIND(SemIR::WhereExpr typed_inst): {
  1800. Phase phase = Phase::Template;
  1801. SemIR::TypeId base_facet_type_id =
  1802. eval_context.insts().Get(typed_inst.period_self_id).type_id();
  1803. SemIR::Inst base_facet_inst =
  1804. eval_context.GetConstantValueAsInst(base_facet_type_id);
  1805. SemIR::FacetTypeInfo info = {.other_requirements = false};
  1806. // `where` provides that the base facet is an error, `type`, or a facet
  1807. // type.
  1808. if (auto facet_type = base_facet_inst.TryAs<SemIR::FacetType>()) {
  1809. info = GetConstantFacetTypeInfo(eval_context, facet_type->facet_type_id,
  1810. &phase);
  1811. } else if (base_facet_type_id == SemIR::ErrorInst::SingletonTypeId) {
  1812. return SemIR::ErrorInst::SingletonConstantId;
  1813. } else {
  1814. CARBON_CHECK(base_facet_type_id == SemIR::TypeType::SingletonTypeId,
  1815. "Unexpected type_id: {0}, inst: {1}", base_facet_type_id,
  1816. base_facet_inst);
  1817. }
  1818. if (typed_inst.requirements_id.has_value()) {
  1819. auto insts = eval_context.inst_blocks().Get(typed_inst.requirements_id);
  1820. for (auto inst_id : insts) {
  1821. if (auto rewrite =
  1822. eval_context.insts().TryGetAs<SemIR::RequirementRewrite>(
  1823. inst_id)) {
  1824. SemIR::ConstantId lhs =
  1825. eval_context.GetConstantValue(rewrite->lhs_id);
  1826. SemIR::ConstantId rhs =
  1827. eval_context.GetConstantValue(rewrite->rhs_id);
  1828. // `where` requirements using `.Self` should not be considered
  1829. // symbolic
  1830. UpdatePhaseIgnorePeriodSelf(eval_context, lhs, &phase);
  1831. UpdatePhaseIgnorePeriodSelf(eval_context, rhs, &phase);
  1832. info.rewrite_constraints.push_back(
  1833. {.lhs_const_id = lhs, .rhs_const_id = rhs});
  1834. } else {
  1835. // TODO: Handle other requirements
  1836. info.other_requirements = true;
  1837. }
  1838. }
  1839. }
  1840. info.Canonicalize();
  1841. return MakeFacetTypeResult(eval_context.context(), info, phase);
  1842. }
  1843. // `not true` -> `false`, `not false` -> `true`.
  1844. // All other uses of unary `not` are non-constant.
  1845. case CARBON_KIND(SemIR::UnaryOperatorNot typed_inst): {
  1846. auto const_id = eval_context.GetConstantValue(typed_inst.operand_id);
  1847. auto phase = GetPhase(eval_context, const_id);
  1848. if (phase == Phase::Template) {
  1849. auto value = eval_context.insts().GetAs<SemIR::BoolLiteral>(
  1850. eval_context.constant_values().GetInstId(const_id));
  1851. return MakeBoolResult(eval_context.context(), value.type_id,
  1852. !value.value.ToBool());
  1853. }
  1854. if (phase == Phase::UnknownDueToError) {
  1855. return SemIR::ErrorInst::SingletonConstantId;
  1856. }
  1857. break;
  1858. }
  1859. // `const (const T)` evaluates to `const T`. Otherwise, `const T` evaluates
  1860. // to itself.
  1861. case CARBON_KIND(SemIR::ConstType typed_inst): {
  1862. auto phase = Phase::Template;
  1863. auto inner_id =
  1864. GetConstantValue(eval_context, typed_inst.inner_id, &phase);
  1865. if (eval_context.context().types().Is<SemIR::ConstType>(inner_id)) {
  1866. return eval_context.context().types().GetConstantId(inner_id);
  1867. }
  1868. typed_inst.inner_id = inner_id;
  1869. return MakeConstantResult(eval_context.context(), typed_inst, phase);
  1870. }
  1871. case CARBON_KIND(SemIR::RequireCompleteType require_complete): {
  1872. auto phase = Phase::Template;
  1873. auto witness_type_id = eval_context.context().GetSingletonType(
  1874. SemIR::WitnessType::SingletonInstId);
  1875. auto complete_type_id = GetConstantValue(
  1876. eval_context, require_complete.complete_type_id, &phase);
  1877. // If the type is a template constant, require it to be complete now.
  1878. if (phase == Phase::Template) {
  1879. if (!eval_context.context().TryToCompleteType(
  1880. complete_type_id, eval_context.GetDiagnosticLoc(inst_id), [&] {
  1881. CARBON_DIAGNOSTIC(IncompleteTypeInMonomorphization, Error,
  1882. "{0} evaluates to incomplete type {1}",
  1883. SemIR::TypeId, SemIR::TypeId);
  1884. return eval_context.emitter().Build(
  1885. eval_context.GetDiagnosticLoc(inst_id),
  1886. IncompleteTypeInMonomorphization,
  1887. require_complete.complete_type_id, complete_type_id);
  1888. })) {
  1889. return SemIR::ErrorInst::SingletonConstantId;
  1890. }
  1891. return MakeConstantResult(
  1892. eval_context.context(),
  1893. SemIR::CompleteTypeWitness{
  1894. .type_id = witness_type_id,
  1895. .object_repr_id =
  1896. eval_context.types().GetObjectRepr(complete_type_id)},
  1897. phase);
  1898. }
  1899. // If it's not a template constant, require it to be complete once it
  1900. // becomes one.
  1901. return MakeConstantResult(
  1902. eval_context.context(),
  1903. SemIR::RequireCompleteType{.type_id = witness_type_id,
  1904. .complete_type_id = complete_type_id},
  1905. phase);
  1906. }
  1907. // These cases are either not expressions or not constant.
  1908. case SemIR::AddrPattern::Kind:
  1909. case SemIR::Assign::Kind:
  1910. case SemIR::BindName::Kind:
  1911. case SemIR::BindingPattern::Kind:
  1912. case SemIR::BlockArg::Kind:
  1913. case SemIR::Branch::Kind:
  1914. case SemIR::BranchIf::Kind:
  1915. case SemIR::BranchWithArg::Kind:
  1916. case SemIR::ImportDecl::Kind:
  1917. case SemIR::NameBindingDecl::Kind:
  1918. case SemIR::OutParam::Kind:
  1919. case SemIR::OutParamPattern::Kind:
  1920. case SemIR::RequirementEquivalent::Kind:
  1921. case SemIR::RequirementImpls::Kind:
  1922. case SemIR::RequirementRewrite::Kind:
  1923. case SemIR::Return::Kind:
  1924. case SemIR::ReturnExpr::Kind:
  1925. case SemIR::ReturnSlotPattern::Kind:
  1926. case SemIR::StructLiteral::Kind:
  1927. case SemIR::TupleLiteral::Kind:
  1928. case SemIR::ValueParam::Kind:
  1929. case SemIR::VarPattern::Kind:
  1930. case SemIR::VarStorage::Kind:
  1931. break;
  1932. case SemIR::ImportRefUnloaded::Kind:
  1933. CARBON_FATAL("ImportRefUnloaded should be loaded before TryEvalInst: {0}",
  1934. inst);
  1935. }
  1936. return SemIR::ConstantId::NotConstant;
  1937. }
  1938. auto TryEvalInst(Context& context, SemIR::InstId inst_id, SemIR::Inst inst)
  1939. -> SemIR::ConstantId {
  1940. EvalContext eval_context(context, inst_id);
  1941. return TryEvalInstInContext(eval_context, inst_id, inst);
  1942. }
  1943. auto TryEvalBlockForSpecific(Context& context, SemIRLoc loc,
  1944. SemIR::SpecificId specific_id,
  1945. SemIR::GenericInstIndex::Region region)
  1946. -> SemIR::InstBlockId {
  1947. auto generic_id = context.specifics().Get(specific_id).generic_id;
  1948. auto eval_block_id = context.generics().Get(generic_id).GetEvalBlock(region);
  1949. auto eval_block = context.inst_blocks().Get(eval_block_id);
  1950. llvm::SmallVector<SemIR::InstId> result;
  1951. result.resize(eval_block.size(), SemIR::InstId::None);
  1952. EvalContext eval_context(context, loc, specific_id,
  1953. SpecificEvalInfo{
  1954. .region = region,
  1955. .values = result,
  1956. });
  1957. DiagnosticAnnotationScope annotate_diagnostics(
  1958. &context.emitter(), [&](auto& builder) {
  1959. CARBON_DIAGNOSTIC(ResolvingSpecificHere, Note, "in {0} used here",
  1960. InstIdAsType);
  1961. builder.Note(loc, ResolvingSpecificHere,
  1962. GetInstForSpecific(context, specific_id));
  1963. });
  1964. for (auto [i, inst_id] : llvm::enumerate(eval_block)) {
  1965. auto const_id = TryEvalInstInContext(eval_context, inst_id,
  1966. context.insts().Get(inst_id));
  1967. result[i] = context.constant_values().GetInstId(const_id);
  1968. CARBON_CHECK(result[i].has_value());
  1969. }
  1970. return context.inst_blocks().Add(result);
  1971. }
  1972. } // namespace Carbon::Check