builtin_function_kind.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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::Invalid, TypeId::Invalid};
  27. };
  28. // Constraint that a type is generic type parameter `I` of the builtin,
  29. // satisfying `TypeConstraint`. See ValidateSignature for details.
  30. template <int I, typename TypeConstraint>
  31. struct TypeParam {
  32. static_assert(I >= 0 && I < MaxTypeParams);
  33. static auto Check(const File& sem_ir, ValidateState& state, TypeId type_id)
  34. -> bool {
  35. if (state.type_params[I].is_valid() && type_id != state.type_params[I]) {
  36. return false;
  37. }
  38. state.type_params[I] = type_id;
  39. return TypeConstraint::Check(sem_ir, state, type_id);
  40. }
  41. };
  42. // Constraint that a type is a specific builtin. See ValidateSignature for
  43. // details.
  44. template <const InstId& BuiltinId>
  45. struct BuiltinType {
  46. static auto Check(const File& sem_ir, ValidateState& /*state*/,
  47. TypeId type_id) -> bool {
  48. return sem_ir.types().GetInstId(type_id) == BuiltinId;
  49. }
  50. };
  51. // Constraint that the function has no return.
  52. struct NoReturn {
  53. static auto Check(const File& sem_ir, ValidateState& /*state*/,
  54. TypeId type_id) -> bool {
  55. auto tuple = sem_ir.types().TryGetAs<SemIR::TupleType>(type_id);
  56. if (!tuple) {
  57. return false;
  58. }
  59. return sem_ir.type_blocks().Get(tuple->elements_id).empty();
  60. }
  61. };
  62. // Constraint that a type is `bool`.
  63. using Bool = BuiltinType<BoolType::SingletonInstId>;
  64. // Constraint that requires the type to be an integer type.
  65. struct AnyInt {
  66. static auto Check(const File& sem_ir, ValidateState& state, TypeId type_id)
  67. -> bool {
  68. if (BuiltinType<IntLiteralType::SingletonInstId>::Check(sem_ir, state,
  69. type_id)) {
  70. return true;
  71. }
  72. return sem_ir.types().Is<IntType>(type_id);
  73. }
  74. };
  75. // Constraint that requires the type to be a float type.
  76. struct AnyFloat {
  77. static auto Check(const File& sem_ir, ValidateState& state, TypeId type_id)
  78. -> bool {
  79. if (BuiltinType<LegacyFloatType::SingletonInstId>::Check(sem_ir, state,
  80. type_id)) {
  81. return true;
  82. }
  83. return sem_ir.types().Is<FloatType>(type_id);
  84. }
  85. };
  86. // Constraint that requires the type to be the type type.
  87. using Type = BuiltinType<TypeType::SingletonInstId>;
  88. } // namespace
  89. // Validates that this builtin has a signature matching the specified signature.
  90. //
  91. // `SignatureFnType` is a C++ function type that describes the signature that is
  92. // expected for this builtin. For example, `auto (AnyInt, AnyInt) -> AnyInt`
  93. // specifies that the builtin takes values of two integer types and returns a
  94. // value of a third integer type. Types used within the signature should provide
  95. // a `Check` function that validates that the Carbon type is expected:
  96. //
  97. // auto Check(const File&, ValidateState&, TypeId) -> bool;
  98. //
  99. // To constrain that the same type is used in multiple places in the signature,
  100. // `TypeParam<I, T>` can be used. For example:
  101. //
  102. // auto (TypeParam<0, AnyInt>, AnyInt) -> TypeParam<0, AnyInt>
  103. //
  104. // describes a builtin that takes two integers, and whose return type matches
  105. // its first parameter type. For convenience, typedefs for `TypeParam<I, T>`
  106. // are used in the descriptions of the builtins.
  107. template <typename SignatureFnType>
  108. static auto ValidateSignature(const File& sem_ir,
  109. llvm::ArrayRef<TypeId> arg_types,
  110. TypeId return_type) -> bool {
  111. using SignatureTraits = llvm::function_traits<SignatureFnType*>;
  112. ValidateState state;
  113. // Must have expected number of arguments.
  114. if (arg_types.size() != SignatureTraits::num_args) {
  115. return false;
  116. }
  117. // Argument types must match.
  118. if (![&]<size_t... Indexes>(std::index_sequence<Indexes...>) {
  119. return ((SignatureTraits::template arg_t<Indexes>::Check(
  120. sem_ir, state, arg_types[Indexes])) &&
  121. ...);
  122. }(std::make_index_sequence<SignatureTraits::num_args>())) {
  123. return false;
  124. }
  125. // Result type must match.
  126. if (!SignatureTraits::result_t::Check(sem_ir, state, return_type)) {
  127. return false;
  128. }
  129. return true;
  130. }
  131. // Descriptions of builtin functions follow. For each builtin, a corresponding
  132. // `BuiltinInfo` constant is declared describing properties of that builtin.
  133. namespace BuiltinFunctionInfo {
  134. // Convenience name used in the builtin type signatures below for a first
  135. // generic type parameter that is constrained to be an integer type.
  136. using IntT = TypeParam<0, AnyInt>;
  137. // Convenience name used in the builtin type signatures below for a second
  138. // generic type parameter that is constrained to be an integer type.
  139. using IntU = TypeParam<1, AnyInt>;
  140. // Convenience name used in the builtin type signatures below for a first
  141. // generic type parameter that is constrained to be an float type.
  142. using FloatT = TypeParam<0, AnyFloat>;
  143. // Not a builtin function.
  144. constexpr BuiltinInfo None = {"", nullptr};
  145. // Prints an argument.
  146. constexpr BuiltinInfo PrintInt = {"print.int",
  147. ValidateSignature<auto(AnyInt)->NoReturn>};
  148. // Returns the `Core.IntLiteral` type.
  149. constexpr BuiltinInfo IntLiteralMakeType = {"int_literal.make_type",
  150. ValidateSignature<auto()->Type>};
  151. // Returns the `iN` type.
  152. // TODO: Should we use a more specific type as the type of the bit width?
  153. constexpr BuiltinInfo IntMakeTypeSigned = {
  154. "int.make_type_signed", ValidateSignature<auto(AnyInt)->Type>};
  155. // Returns the `uN` type.
  156. constexpr BuiltinInfo IntMakeTypeUnsigned = {
  157. "int.make_type_unsigned", ValidateSignature<auto(AnyInt)->Type>};
  158. // Returns float types, such as `f64`. Currently only supports `f64`.
  159. constexpr BuiltinInfo FloatMakeType = {"float.make_type",
  160. ValidateSignature<auto(AnyInt)->Type>};
  161. // Returns the `bool` type.
  162. constexpr BuiltinInfo BoolMakeType = {"bool.make_type",
  163. ValidateSignature<auto()->Type>};
  164. // Converts between integer types, with a diagnostic if the value doesn't fit.
  165. constexpr BuiltinInfo IntConvertChecked = {
  166. "int.convert_checked", ValidateSignature<auto(AnyInt)->AnyInt>};
  167. // "int.snegate": integer negation.
  168. constexpr BuiltinInfo IntSNegate = {"int.snegate",
  169. ValidateSignature<auto(IntT)->IntT>};
  170. // "int.sadd": integer addition.
  171. constexpr BuiltinInfo IntSAdd = {"int.sadd",
  172. ValidateSignature<auto(IntT, IntT)->IntT>};
  173. // "int.ssub": integer subtraction.
  174. constexpr BuiltinInfo IntSSub = {"int.ssub",
  175. ValidateSignature<auto(IntT, IntT)->IntT>};
  176. // "int.smul": integer multiplication.
  177. constexpr BuiltinInfo IntSMul = {"int.smul",
  178. ValidateSignature<auto(IntT, IntT)->IntT>};
  179. // "int.sdiv": integer division.
  180. constexpr BuiltinInfo IntSDiv = {"int.sdiv",
  181. ValidateSignature<auto(IntT, IntT)->IntT>};
  182. // "int.smod": integer modulo.
  183. constexpr BuiltinInfo IntSMod = {"int.smod",
  184. ValidateSignature<auto(IntT, IntT)->IntT>};
  185. // "int.unegate": unsigned integer negation.
  186. constexpr BuiltinInfo IntUNegate = {"int.unegate",
  187. ValidateSignature<auto(IntT)->IntT>};
  188. // "int.uadd": unsigned integer addition.
  189. constexpr BuiltinInfo IntUAdd = {"int.uadd",
  190. ValidateSignature<auto(IntT, IntT)->IntT>};
  191. // "int.usub": unsigned integer subtraction.
  192. constexpr BuiltinInfo IntUSub = {"int.usub",
  193. ValidateSignature<auto(IntT, IntT)->IntT>};
  194. // "int.umul": unsigned integer multiplication.
  195. constexpr BuiltinInfo IntUMul = {"int.umul",
  196. ValidateSignature<auto(IntT, IntT)->IntT>};
  197. // "int.udiv": unsigned integer division.
  198. constexpr BuiltinInfo IntUDiv = {"int.udiv",
  199. ValidateSignature<auto(IntT, IntT)->IntT>};
  200. // "int.mod": integer modulo.
  201. constexpr BuiltinInfo IntUMod = {"int.umod",
  202. ValidateSignature<auto(IntT, IntT)->IntT>};
  203. // "int.complement": integer bitwise complement.
  204. constexpr BuiltinInfo IntComplement = {"int.complement",
  205. ValidateSignature<auto(IntT)->IntT>};
  206. // "int.and": integer bitwise and.
  207. constexpr BuiltinInfo IntAnd = {"int.and",
  208. ValidateSignature<auto(IntT, IntT)->IntT>};
  209. // "int.or": integer bitwise or.
  210. constexpr BuiltinInfo IntOr = {"int.or",
  211. ValidateSignature<auto(IntT, IntT)->IntT>};
  212. // "int.xor": integer bitwise xor.
  213. constexpr BuiltinInfo IntXor = {"int.xor",
  214. ValidateSignature<auto(IntT, IntT)->IntT>};
  215. // "int.left_shift": integer left shift.
  216. constexpr BuiltinInfo IntLeftShift = {
  217. "int.left_shift", ValidateSignature<auto(IntT, IntU)->IntT>};
  218. // "int.left_shift": integer right shift.
  219. constexpr BuiltinInfo IntRightShift = {
  220. "int.right_shift", ValidateSignature<auto(IntT, IntU)->IntT>};
  221. // "int.eq": integer equality comparison.
  222. constexpr BuiltinInfo IntEq = {"int.eq",
  223. ValidateSignature<auto(IntT, IntT)->Bool>};
  224. // "int.neq": integer non-equality comparison.
  225. constexpr BuiltinInfo IntNeq = {"int.neq",
  226. ValidateSignature<auto(IntT, IntT)->Bool>};
  227. // "int.less": integer less than comparison.
  228. constexpr BuiltinInfo IntLess = {"int.less",
  229. ValidateSignature<auto(IntT, IntT)->Bool>};
  230. // "int.less_eq": integer less than or equal comparison.
  231. constexpr BuiltinInfo IntLessEq = {"int.less_eq",
  232. ValidateSignature<auto(IntT, IntT)->Bool>};
  233. // "int.greater": integer greater than comparison.
  234. constexpr BuiltinInfo IntGreater = {"int.greater",
  235. ValidateSignature<auto(IntT, IntT)->Bool>};
  236. // "int.greater_eq": integer greater than or equal comparison.
  237. constexpr BuiltinInfo IntGreaterEq = {
  238. "int.greater_eq", ValidateSignature<auto(IntT, IntT)->Bool>};
  239. // "float.negate": float negation.
  240. constexpr BuiltinInfo FloatNegate = {"float.negate",
  241. ValidateSignature<auto(FloatT)->FloatT>};
  242. // "float.add": float addition.
  243. constexpr BuiltinInfo FloatAdd = {
  244. "float.add", ValidateSignature<auto(FloatT, FloatT)->FloatT>};
  245. // "float.sub": float subtraction.
  246. constexpr BuiltinInfo FloatSub = {
  247. "float.sub", ValidateSignature<auto(FloatT, FloatT)->FloatT>};
  248. // "float.mul": float multiplication.
  249. constexpr BuiltinInfo FloatMul = {
  250. "float.mul", ValidateSignature<auto(FloatT, FloatT)->FloatT>};
  251. // "float.div": float division.
  252. constexpr BuiltinInfo FloatDiv = {
  253. "float.div", ValidateSignature<auto(FloatT, FloatT)->FloatT>};
  254. // "float.eq": float equality comparison.
  255. constexpr BuiltinInfo FloatEq = {"float.eq",
  256. ValidateSignature<auto(FloatT, FloatT)->Bool>};
  257. // "float.neq": float non-equality comparison.
  258. constexpr BuiltinInfo FloatNeq = {
  259. "float.neq", ValidateSignature<auto(FloatT, FloatT)->Bool>};
  260. // "float.less": float less than comparison.
  261. constexpr BuiltinInfo FloatLess = {
  262. "float.less", ValidateSignature<auto(FloatT, FloatT)->Bool>};
  263. // "float.less_eq": float less than or equal comparison.
  264. constexpr BuiltinInfo FloatLessEq = {
  265. "float.less_eq", ValidateSignature<auto(FloatT, FloatT)->Bool>};
  266. // "float.greater": float greater than comparison.
  267. constexpr BuiltinInfo FloatGreater = {
  268. "float.greater", ValidateSignature<auto(FloatT, FloatT)->Bool>};
  269. // "float.greater_eq": float greater than or equal comparison.
  270. constexpr BuiltinInfo FloatGreaterEq = {
  271. "float.greater_eq", ValidateSignature<auto(FloatT, FloatT)->Bool>};
  272. } // namespace BuiltinFunctionInfo
  273. CARBON_DEFINE_ENUM_CLASS_NAMES(BuiltinFunctionKind) = {
  274. #define CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(Name) \
  275. BuiltinFunctionInfo::Name.name,
  276. #include "toolchain/sem_ir/builtin_function_kind.def"
  277. };
  278. // Returns the builtin function kind with the given name, or None if the name
  279. // is unknown.
  280. auto BuiltinFunctionKind::ForBuiltinName(llvm::StringRef name)
  281. -> BuiltinFunctionKind {
  282. #define CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(Name) \
  283. if (name == BuiltinFunctionInfo::Name.name) { \
  284. return BuiltinFunctionKind::Name; \
  285. }
  286. #include "toolchain/sem_ir/builtin_function_kind.def"
  287. return BuiltinFunctionKind::None;
  288. }
  289. auto BuiltinFunctionKind::IsValidType(const File& sem_ir,
  290. llvm::ArrayRef<TypeId> arg_types,
  291. TypeId return_type) const -> bool {
  292. static constexpr ValidateFn* ValidateFns[] = {
  293. #define CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(Name) \
  294. BuiltinFunctionInfo::Name.validate,
  295. #include "toolchain/sem_ir/builtin_function_kind.def"
  296. };
  297. return ValidateFns[AsInt()](sem_ir, arg_types, return_type);
  298. }
  299. auto BuiltinFunctionKind::IsCompTimeOnly() const -> bool {
  300. return *this == BuiltinFunctionKind::IntConvertChecked;
  301. }
  302. } // namespace Carbon::SemIR