builtin_function_kind.cpp 14 KB

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