builtin_function_kind.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  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/sem_ir/builtin_function_kind.h"
  5. #include <utility>
  6. #include "toolchain/sem_ir/file.h"
  7. #include "toolchain/sem_ir/ids.h"
  8. #include "toolchain/sem_ir/typed_insts.h"
  9. namespace Carbon::SemIR {
  10. // A function that validates that a builtin was declared properly.
  11. using ValidateFn = auto(const File& sem_ir, llvm::ArrayRef<TypeId> arg_types,
  12. TypeId return_type) -> bool;
  13. namespace {
  14. // Information about a builtin function.
  15. struct BuiltinInfo {
  16. llvm::StringLiteral name;
  17. ValidateFn* validate;
  18. };
  19. // The maximum number of type parameters any builtin needs.
  20. constexpr int MaxTypeParams = 2;
  21. // State used when validating a builtin signature that persists between
  22. // individual checks.
  23. struct ValidateState {
  24. // The type values of type parameters in the builtin signature. Invalid if
  25. // either no value has been deduced yet or the parameter is not used.
  26. TypeId type_params[MaxTypeParams] = {TypeId::None, TypeId::None};
  27. };
  28. template <typename TypeConstraint>
  29. auto Check(const File& sem_ir, ValidateState& state, TypeId type_id) -> bool;
  30. // Constraint that a type is generic type parameter `I` of the builtin,
  31. // satisfying `TypeConstraint`. See ValidateSignature for details.
  32. template <int I, typename TypeConstraint>
  33. struct TypeParam {
  34. static_assert(I >= 0 && I < MaxTypeParams);
  35. static auto Check(const File& sem_ir, ValidateState& state, TypeId type_id)
  36. -> bool {
  37. if (state.type_params[I].has_value() && type_id != state.type_params[I]) {
  38. return false;
  39. }
  40. if (!TypeConstraint::Check(sem_ir, state, type_id)) {
  41. return false;
  42. }
  43. state.type_params[I] = type_id;
  44. return true;
  45. }
  46. };
  47. // Constraint that a type is a specific builtin. See ValidateSignature for
  48. // details.
  49. template <const TypeInstId& BuiltinId>
  50. struct BuiltinType {
  51. static auto Check(const File& sem_ir, ValidateState& /*state*/,
  52. TypeId type_id) -> bool {
  53. return sem_ir.types().GetInstId(type_id) == BuiltinId;
  54. }
  55. };
  56. // Constraint that a type is a pointer to another type. See ValidateSignature
  57. // for details.
  58. template <typename PointeeT>
  59. struct PointerTo {
  60. static auto Check(const File& sem_ir, ValidateState& state, TypeId type_id)
  61. -> bool {
  62. if (!sem_ir.types().Is<SemIR::PointerType>(type_id)) {
  63. return false;
  64. }
  65. return SemIR::Check<PointeeT>(sem_ir, state,
  66. sem_ir.GetPointeeType(type_id));
  67. }
  68. };
  69. // Constraint that a type is `()`, used as the return type of builtin functions
  70. // with no return value.
  71. struct NoReturn {
  72. static auto Check(const File& sem_ir, ValidateState& /*state*/,
  73. TypeId type_id) -> bool {
  74. auto tuple = sem_ir.types().TryGetAs<SemIR::TupleType>(type_id);
  75. if (!tuple) {
  76. return false;
  77. }
  78. return sem_ir.inst_blocks().Get(tuple->type_elements_id).empty();
  79. }
  80. };
  81. // Constraint that a type is `bool`.
  82. using Bool = BuiltinType<BoolType::TypeInstId>;
  83. // Constraint that requires the type to be a sized integer type.
  84. struct AnySizedInt {
  85. static auto Check(const File& sem_ir, ValidateState& /*state*/,
  86. TypeId type_id) -> bool {
  87. return sem_ir.types().Is<IntType>(type_id);
  88. }
  89. };
  90. // Constraint that requires the type to be an integer type.
  91. struct AnyInt {
  92. static auto Check(const File& sem_ir, ValidateState& state, TypeId type_id)
  93. -> bool {
  94. return AnySizedInt::Check(sem_ir, state, type_id) ||
  95. BuiltinType<IntLiteralType::TypeInstId>::Check(sem_ir, state,
  96. type_id);
  97. }
  98. };
  99. // Constraint that requires the type to be a float type.
  100. struct AnyFloat {
  101. static auto Check(const File& sem_ir, ValidateState& state, TypeId type_id)
  102. -> bool {
  103. if (BuiltinType<LegacyFloatType::TypeInstId>::Check(sem_ir, state,
  104. type_id)) {
  105. return true;
  106. }
  107. return sem_ir.types().Is<FloatType>(type_id);
  108. }
  109. };
  110. // Constraint that requires the type to be the type type.
  111. using Type = BuiltinType<TypeType::TypeInstId>;
  112. // Constraint that requires the type to be a type value, whose type is type
  113. // type. Also accepts symbolic constant value types.
  114. struct AnyType {
  115. static auto Check(const File& sem_ir, ValidateState& state, TypeId type_id)
  116. -> bool {
  117. if (BuiltinType<TypeType::TypeInstId>::Check(sem_ir, state, type_id)) {
  118. return true;
  119. }
  120. return sem_ir.types().GetAsInst(type_id).type_id() == TypeType::TypeId;
  121. }
  122. };
  123. // Checks that the specified type matches the given type constraint.
  124. template <typename TypeConstraint>
  125. auto Check(const File& sem_ir, ValidateState& state, TypeId type_id) -> bool {
  126. while (type_id.has_value()) {
  127. // Allow a type that satisfies the constraint.
  128. if (TypeConstraint::Check(sem_ir, state, type_id)) {
  129. return true;
  130. }
  131. // Also allow a class type that adapts a matching type.
  132. auto class_type = sem_ir.types().TryGetAs<ClassType>(type_id);
  133. if (!class_type) {
  134. break;
  135. }
  136. type_id = sem_ir.classes()
  137. .Get(class_type->class_id)
  138. .GetAdaptedType(sem_ir, class_type->specific_id);
  139. }
  140. return false;
  141. }
  142. } // namespace
  143. // Validates that this builtin has a signature matching the specified signature.
  144. //
  145. // `SignatureFnType` is a C++ function type that describes the signature that is
  146. // expected for this builtin. For example, `auto (AnyInt, AnyInt) -> AnyInt`
  147. // specifies that the builtin takes values of two integer types and returns a
  148. // value of a third integer type. Types used within the signature should provide
  149. // a `Check` function that validates that the Carbon type is expected:
  150. //
  151. // auto Check(const File&, ValidateState&, TypeId) -> bool;
  152. //
  153. // To constrain that the same type is used in multiple places in the signature,
  154. // `TypeParam<I, T>` can be used. For example:
  155. //
  156. // auto (TypeParam<0, AnyInt>, AnyInt) -> TypeParam<0, AnyInt>
  157. //
  158. // describes a builtin that takes two integers, and whose return type matches
  159. // its first parameter type. For convenience, typedefs for `TypeParam<I, T>`
  160. // are used in the descriptions of the builtins.
  161. template <typename SignatureFnType>
  162. static auto ValidateSignature(const File& sem_ir,
  163. llvm::ArrayRef<TypeId> arg_types,
  164. TypeId return_type) -> bool {
  165. using SignatureTraits = llvm::function_traits<SignatureFnType*>;
  166. ValidateState state;
  167. // Must have expected number of arguments.
  168. if (arg_types.size() != SignatureTraits::num_args) {
  169. return false;
  170. }
  171. // Argument types must match.
  172. if (![&]<size_t... Indexes>(std::index_sequence<Indexes...>) {
  173. return ((Check<typename SignatureTraits::template arg_t<Indexes>>(
  174. sem_ir, state, arg_types[Indexes])) &&
  175. ...);
  176. }(std::make_index_sequence<SignatureTraits::num_args>())) {
  177. return false;
  178. }
  179. // Result type must match.
  180. if (!Check<typename SignatureTraits::result_t>(sem_ir, state, return_type)) {
  181. return false;
  182. }
  183. return true;
  184. }
  185. // Validates the signature for NoOp. This ignores all arguments, only validating
  186. // that the return type is compatible.
  187. static auto ValidateNoOpSignature(const File& sem_ir,
  188. llvm::ArrayRef<TypeId> /*arg_types*/,
  189. TypeId return_type) -> bool {
  190. ValidateState state;
  191. return Check<NoReturn>(sem_ir, state, return_type);
  192. }
  193. // Descriptions of builtin functions follow. For each builtin, a corresponding
  194. // `BuiltinInfo` constant is declared describing properties of that builtin.
  195. namespace BuiltinFunctionInfo {
  196. // Convenience name used in the builtin type signatures below for a first
  197. // generic type parameter that is constrained to be an integer type.
  198. using IntT = TypeParam<0, AnyInt>;
  199. // Convenience name used in the builtin type signatures below for a second
  200. // generic type parameter that is constrained to be an integer type.
  201. using IntU = TypeParam<1, AnyInt>;
  202. // Convenience name used in the builtin type signatures below for a first
  203. // generic type parameter that is constrained to be a sized integer type.
  204. using SizedIntT = TypeParam<0, AnySizedInt>;
  205. // Convenience name used in the builtin type signatures below for a second
  206. // generic type parameter that is constrained to be a sized integer type.
  207. using SizedIntU = TypeParam<1, AnySizedInt>;
  208. // Convenience name used in the builtin type signatures below for a first
  209. // generic type parameter that is constrained to be an float type.
  210. using FloatT = TypeParam<0, AnyFloat>;
  211. // Not a builtin function.
  212. constexpr BuiltinInfo None = {"", nullptr};
  213. constexpr BuiltinInfo NoOp = {"no_op", ValidateNoOpSignature};
  214. // Prints a single character.
  215. constexpr BuiltinInfo PrintChar = {
  216. "print.char", ValidateSignature<auto(AnySizedInt)->AnySizedInt>};
  217. // Prints an integer.
  218. constexpr BuiltinInfo PrintInt = {
  219. "print.int", ValidateSignature<auto(AnySizedInt)->NoReturn>};
  220. // Reads a single character from stdin.
  221. constexpr BuiltinInfo ReadChar = {"read.char",
  222. ValidateSignature<auto()->AnySizedInt>};
  223. // Returns the `Core.IntLiteral` type.
  224. constexpr BuiltinInfo IntLiteralMakeType = {"int_literal.make_type",
  225. ValidateSignature<auto()->Type>};
  226. // Returns the `iN` type.
  227. // TODO: Should we use a more specific type as the type of the bit width?
  228. constexpr BuiltinInfo IntMakeTypeSigned = {
  229. "int.make_type_signed", ValidateSignature<auto(AnyInt)->Type>};
  230. // Returns the `uN` type.
  231. constexpr BuiltinInfo IntMakeTypeUnsigned = {
  232. "int.make_type_unsigned", ValidateSignature<auto(AnyInt)->Type>};
  233. // Returns float types, such as `f64`. Currently only supports `f64`.
  234. constexpr BuiltinInfo FloatMakeType = {"float.make_type",
  235. ValidateSignature<auto(AnyInt)->Type>};
  236. // Returns the `bool` type.
  237. constexpr BuiltinInfo BoolMakeType = {"bool.make_type",
  238. ValidateSignature<auto()->Type>};
  239. // Converts between integer types, truncating if necessary.
  240. constexpr BuiltinInfo IntConvert = {"int.convert",
  241. ValidateSignature<auto(AnyInt)->AnyInt>};
  242. // Converts between integer types, with a diagnostic if the value doesn't fit.
  243. constexpr BuiltinInfo IntConvertChecked = {
  244. "int.convert_checked", ValidateSignature<auto(AnyInt)->AnyInt>};
  245. // "int.snegate": integer negation.
  246. constexpr BuiltinInfo IntSNegate = {"int.snegate",
  247. ValidateSignature<auto(IntT)->IntT>};
  248. // "int.sadd": integer addition.
  249. constexpr BuiltinInfo IntSAdd = {"int.sadd",
  250. ValidateSignature<auto(IntT, IntT)->IntT>};
  251. // "int.ssub": integer subtraction.
  252. constexpr BuiltinInfo IntSSub = {"int.ssub",
  253. ValidateSignature<auto(IntT, IntT)->IntT>};
  254. // "int.smul": integer multiplication.
  255. constexpr BuiltinInfo IntSMul = {"int.smul",
  256. ValidateSignature<auto(IntT, IntT)->IntT>};
  257. // "int.sdiv": integer division.
  258. constexpr BuiltinInfo IntSDiv = {"int.sdiv",
  259. ValidateSignature<auto(IntT, IntT)->IntT>};
  260. // "int.smod": integer modulo.
  261. constexpr BuiltinInfo IntSMod = {"int.smod",
  262. ValidateSignature<auto(IntT, IntT)->IntT>};
  263. // "int.unegate": unsigned integer negation.
  264. constexpr BuiltinInfo IntUNegate = {
  265. "int.unegate", ValidateSignature<auto(SizedIntT)->SizedIntT>};
  266. // "int.uadd": unsigned integer addition.
  267. constexpr BuiltinInfo IntUAdd = {
  268. "int.uadd", ValidateSignature<auto(SizedIntT, SizedIntT)->SizedIntT>};
  269. // "int.usub": unsigned integer subtraction.
  270. constexpr BuiltinInfo IntUSub = {
  271. "int.usub", ValidateSignature<auto(SizedIntT, SizedIntT)->SizedIntT>};
  272. // "int.umul": unsigned integer multiplication.
  273. constexpr BuiltinInfo IntUMul = {
  274. "int.umul", ValidateSignature<auto(SizedIntT, SizedIntT)->SizedIntT>};
  275. // "int.udiv": unsigned integer division.
  276. constexpr BuiltinInfo IntUDiv = {
  277. "int.udiv", ValidateSignature<auto(SizedIntT, SizedIntT)->SizedIntT>};
  278. // "int.mod": integer modulo.
  279. constexpr BuiltinInfo IntUMod = {
  280. "int.umod", ValidateSignature<auto(SizedIntT, SizedIntT)->SizedIntT>};
  281. // "int.complement": integer bitwise complement.
  282. constexpr BuiltinInfo IntComplement = {"int.complement",
  283. ValidateSignature<auto(IntT)->IntT>};
  284. // "int.and": integer bitwise and.
  285. constexpr BuiltinInfo IntAnd = {"int.and",
  286. ValidateSignature<auto(IntT, IntT)->IntT>};
  287. // "int.or": integer bitwise or.
  288. constexpr BuiltinInfo IntOr = {"int.or",
  289. ValidateSignature<auto(IntT, IntT)->IntT>};
  290. // "int.xor": integer bitwise xor.
  291. constexpr BuiltinInfo IntXor = {"int.xor",
  292. ValidateSignature<auto(IntT, IntT)->IntT>};
  293. // "int.left_shift": integer left shift.
  294. constexpr BuiltinInfo IntLeftShift = {
  295. "int.left_shift", ValidateSignature<auto(IntT, IntU)->IntT>};
  296. // "int.right_shift": integer right shift.
  297. constexpr BuiltinInfo IntRightShift = {
  298. "int.right_shift", ValidateSignature<auto(IntT, IntU)->IntT>};
  299. // "int.sadd_assign": integer in-place addition.
  300. constexpr BuiltinInfo IntSAddAssign = {
  301. "int.sadd_assign",
  302. ValidateSignature<auto(PointerTo<SizedIntT>, SizedIntT)->NoReturn>};
  303. // "int.ssub_assign": integer in-place subtraction.
  304. constexpr BuiltinInfo IntSSubAssign = {
  305. "int.ssub_assign",
  306. ValidateSignature<auto(PointerTo<SizedIntT>, SizedIntT)->NoReturn>};
  307. // "int.smul_assign": integer in-place multiplication.
  308. constexpr BuiltinInfo IntSMulAssign = {
  309. "int.smul_assign",
  310. ValidateSignature<auto(PointerTo<SizedIntT>, SizedIntT)->NoReturn>};
  311. // "int.sdiv_assign": integer in-place division.
  312. constexpr BuiltinInfo IntSDivAssign = {
  313. "int.sdiv_assign",
  314. ValidateSignature<auto(PointerTo<SizedIntT>, SizedIntT)->NoReturn>};
  315. // "int.smod_assign": integer in-place modulo.
  316. constexpr BuiltinInfo IntSModAssign = {
  317. "int.smod_assign",
  318. ValidateSignature<auto(PointerTo<SizedIntT>, SizedIntT)->NoReturn>};
  319. // "int.uadd_assign": unsigned integer in-place addition.
  320. constexpr BuiltinInfo IntUAddAssign = {
  321. "int.uadd_assign",
  322. ValidateSignature<auto(PointerTo<SizedIntT>, SizedIntT)->NoReturn>};
  323. // "int.usub_assign": unsigned integer in-place subtraction.
  324. constexpr BuiltinInfo IntUSubAssign = {
  325. "int.usub_assign",
  326. ValidateSignature<auto(PointerTo<SizedIntT>, SizedIntT)->NoReturn>};
  327. // "int.umul_assign": unsigned integer in-place multiplication.
  328. constexpr BuiltinInfo IntUMulAssign = {
  329. "int.umul_assign",
  330. ValidateSignature<auto(PointerTo<SizedIntT>, SizedIntT)->NoReturn>};
  331. // "int.udiv_assign": unsigned integer in-place division.
  332. constexpr BuiltinInfo IntUDivAssign = {
  333. "int.udiv_assign",
  334. ValidateSignature<auto(PointerTo<SizedIntT>, SizedIntT)->NoReturn>};
  335. // "int.mod_assign": integer in-place modulo.
  336. constexpr BuiltinInfo IntUModAssign = {
  337. "int.umod_assign",
  338. ValidateSignature<auto(PointerTo<SizedIntT>, SizedIntT)->NoReturn>};
  339. // "int.and_assign": integer in-place bitwise and.
  340. constexpr BuiltinInfo IntAndAssign = {
  341. "int.and_assign",
  342. ValidateSignature<auto(PointerTo<SizedIntT>, SizedIntT)->NoReturn>};
  343. // "int.or_assign": integer in-place bitwise or.
  344. constexpr BuiltinInfo IntOrAssign = {
  345. "int.or_assign",
  346. ValidateSignature<auto(PointerTo<SizedIntT>, SizedIntT)->NoReturn>};
  347. // "int.xor_assign": integer in-place bitwise xor.
  348. constexpr BuiltinInfo IntXorAssign = {
  349. "int.xor_assign",
  350. ValidateSignature<auto(PointerTo<SizedIntT>, SizedIntT)->NoReturn>};
  351. // "int.left_shift_assign": integer in-place left shift.
  352. constexpr BuiltinInfo IntLeftShiftAssign = {
  353. "int.left_shift_assign",
  354. ValidateSignature<auto(PointerTo<SizedIntT>, SizedIntU)->NoReturn>};
  355. // "int.right_shift_assign": integer in-place right shift.
  356. constexpr BuiltinInfo IntRightShiftAssign = {
  357. "int.right_shift_assign",
  358. ValidateSignature<auto(PointerTo<SizedIntT>, SizedIntU)->NoReturn>};
  359. // "int.eq": integer equality comparison.
  360. constexpr BuiltinInfo IntEq = {"int.eq",
  361. ValidateSignature<auto(IntT, IntU)->Bool>};
  362. // "int.neq": integer non-equality comparison.
  363. constexpr BuiltinInfo IntNeq = {"int.neq",
  364. ValidateSignature<auto(IntT, IntU)->Bool>};
  365. // "int.less": integer less than comparison.
  366. constexpr BuiltinInfo IntLess = {"int.less",
  367. ValidateSignature<auto(IntT, IntU)->Bool>};
  368. // "int.less_eq": integer less than or equal comparison.
  369. constexpr BuiltinInfo IntLessEq = {"int.less_eq",
  370. ValidateSignature<auto(IntT, IntU)->Bool>};
  371. // "int.greater": integer greater than comparison.
  372. constexpr BuiltinInfo IntGreater = {"int.greater",
  373. ValidateSignature<auto(IntT, IntU)->Bool>};
  374. // "int.greater_eq": integer greater than or equal comparison.
  375. constexpr BuiltinInfo IntGreaterEq = {
  376. "int.greater_eq", ValidateSignature<auto(IntT, IntU)->Bool>};
  377. // "float.negate": float negation.
  378. constexpr BuiltinInfo FloatNegate = {"float.negate",
  379. ValidateSignature<auto(FloatT)->FloatT>};
  380. // "float.add": float addition.
  381. constexpr BuiltinInfo FloatAdd = {
  382. "float.add", ValidateSignature<auto(FloatT, FloatT)->FloatT>};
  383. // "float.sub": float subtraction.
  384. constexpr BuiltinInfo FloatSub = {
  385. "float.sub", ValidateSignature<auto(FloatT, FloatT)->FloatT>};
  386. // "float.mul": float multiplication.
  387. constexpr BuiltinInfo FloatMul = {
  388. "float.mul", ValidateSignature<auto(FloatT, FloatT)->FloatT>};
  389. // "float.div": float division.
  390. constexpr BuiltinInfo FloatDiv = {
  391. "float.div", ValidateSignature<auto(FloatT, FloatT)->FloatT>};
  392. // "float.eq": float equality comparison.
  393. constexpr BuiltinInfo FloatEq = {"float.eq",
  394. ValidateSignature<auto(FloatT, FloatT)->Bool>};
  395. // "float.neq": float non-equality comparison.
  396. constexpr BuiltinInfo FloatNeq = {
  397. "float.neq", ValidateSignature<auto(FloatT, FloatT)->Bool>};
  398. // "float.less": float less than comparison.
  399. constexpr BuiltinInfo FloatLess = {
  400. "float.less", ValidateSignature<auto(FloatT, FloatT)->Bool>};
  401. // "float.less_eq": float less than or equal comparison.
  402. constexpr BuiltinInfo FloatLessEq = {
  403. "float.less_eq", ValidateSignature<auto(FloatT, FloatT)->Bool>};
  404. // "float.greater": float greater than comparison.
  405. constexpr BuiltinInfo FloatGreater = {
  406. "float.greater", ValidateSignature<auto(FloatT, FloatT)->Bool>};
  407. // "float.greater_eq": float greater than or equal comparison.
  408. constexpr BuiltinInfo FloatGreaterEq = {
  409. "float.greater_eq", ValidateSignature<auto(FloatT, FloatT)->Bool>};
  410. // "bool.eq": bool equality comparison.
  411. constexpr BuiltinInfo BoolEq = {"bool.eq",
  412. ValidateSignature<auto(Bool, Bool)->Bool>};
  413. // "bool.neq": bool non-equality comparison.
  414. constexpr BuiltinInfo BoolNeq = {"bool.neq",
  415. ValidateSignature<auto(Bool, Bool)->Bool>};
  416. // "type.and": facet type combination.
  417. constexpr BuiltinInfo TypeAnd = {
  418. "type.and", ValidateSignature<auto(AnyType, AnyType)->AnyType>};
  419. } // namespace BuiltinFunctionInfo
  420. CARBON_DEFINE_ENUM_CLASS_NAMES(BuiltinFunctionKind) = {
  421. #define CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(Name) \
  422. BuiltinFunctionInfo::Name.name,
  423. #include "toolchain/sem_ir/builtin_function_kind.def"
  424. };
  425. // Returns the builtin function kind with the given name, or None if the name
  426. // is unknown.
  427. auto BuiltinFunctionKind::ForBuiltinName(llvm::StringRef name)
  428. -> BuiltinFunctionKind {
  429. #define CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(Name) \
  430. if (name == BuiltinFunctionInfo::Name.name) { \
  431. return BuiltinFunctionKind::Name; \
  432. }
  433. #include "toolchain/sem_ir/builtin_function_kind.def"
  434. return BuiltinFunctionKind::None;
  435. }
  436. auto BuiltinFunctionKind::IsValidType(const File& sem_ir,
  437. llvm::ArrayRef<TypeId> arg_types,
  438. TypeId return_type) const -> bool {
  439. static constexpr ValidateFn* ValidateFns[] = {
  440. #define CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(Name) \
  441. BuiltinFunctionInfo::Name.validate,
  442. #include "toolchain/sem_ir/builtin_function_kind.def"
  443. };
  444. return ValidateFns[AsInt()](sem_ir, arg_types, return_type);
  445. }
  446. // Determines whether a builtin call involves an integer literal in its
  447. // arguments or return type. If so, for many builtins we want to treat the call
  448. // as being compile-time-only. This is because `Core.IntLiteral` has an empty
  449. // runtime representation, and a value of that type isn't necessarily a
  450. // compile-time constant, so an arbitrary runtime value of type
  451. // `Core.IntLiteral` may not have a value available for the builtin to use. For
  452. // example, given:
  453. //
  454. // var n: Core.IntLiteral() = 123;
  455. //
  456. // we would be unable to lower a runtime operation such as `(1 as i32) << n`
  457. // because the runtime representation of `n` doesn't track its value at all.
  458. //
  459. // For now, we treat all operations involving `Core.IntLiteral` as being
  460. // compile-time-only.
  461. //
  462. // TODO: We will need to accept things like `some_i32 << 5` eventually. We could
  463. // allow builtin calls at runtime if all the IntLiteral arguments have constant
  464. // values, or add logic to the prelude to promote the `IntLiteral` operand to a
  465. // different type in such cases.
  466. //
  467. // TODO: For now, we also treat builtins *returning* `Core.IntLiteral` as being
  468. // compile-time-only. This is mostly done for simplicity, but should probably be
  469. // revisited.
  470. static auto AnyIntLiteralTypes(const File& sem_ir,
  471. llvm::ArrayRef<InstId> arg_ids,
  472. TypeId return_type_id) -> bool {
  473. if (sem_ir.types().Is<SemIR::IntLiteralType>(return_type_id)) {
  474. return true;
  475. }
  476. for (auto arg_id : arg_ids) {
  477. if (sem_ir.types().Is<SemIR::IntLiteralType>(
  478. sem_ir.insts().Get(arg_id).type_id())) {
  479. return true;
  480. }
  481. }
  482. return false;
  483. }
  484. auto BuiltinFunctionKind::IsCompTimeOnly(const File& sem_ir,
  485. llvm::ArrayRef<InstId> arg_ids,
  486. TypeId return_type_id) const -> bool {
  487. switch (*this) {
  488. case IntConvertChecked:
  489. // Checked integer conversions are compile-time only.
  490. return true;
  491. case IntConvert:
  492. case IntSNegate:
  493. case IntComplement:
  494. case IntSAdd:
  495. case IntSSub:
  496. case IntSMul:
  497. case IntSDiv:
  498. case IntSMod:
  499. case IntAnd:
  500. case IntOr:
  501. case IntXor:
  502. case IntLeftShift:
  503. case IntRightShift:
  504. case IntEq:
  505. case IntNeq:
  506. case IntLess:
  507. case IntLessEq:
  508. case IntGreater:
  509. case IntGreaterEq:
  510. // Integer operations are compile-time-only if they involve integer
  511. // literal types. See AnyIntLiteralTypes comment for explanation.
  512. return AnyIntLiteralTypes(sem_ir, arg_ids, return_type_id);
  513. case TypeAnd:
  514. return true;
  515. default:
  516. // TODO: Should the sized MakeType functions be compile-time only? We
  517. // can't produce diagnostics for bad sizes at runtime.
  518. return false;
  519. }
  520. }
  521. } // namespace Carbon::SemIR