builtin_function_kind.cpp 23 KB

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