Builtin.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #ifndef QLOW_SEM_BUILTIN_H
  2. #define QLOW_SEM_BUILTIN_H
  3. #include "Semantic.h"
  4. #include "Scope.h"
  5. #include "Type.h"
  6. #include <llvm/IR/Value.h>
  7. #include <llvm/IR/IRBuilder.h>
  8. namespace qlow
  9. {
  10. namespace sem
  11. {
  12. class Context;
  13. NativeScope generateNativeScope(Context& context);
  14. void fillNativeScope(NativeScope& scope);
  15. NativeTypeScope generateNativeTypeScope(Context& context, Type::Native native);
  16. struct NativeMethod;
  17. struct UnaryNativeMethod;
  18. struct BinaryNativeMethod;
  19. }
  20. }
  21. struct qlow::sem::NativeMethod : public sem::Method
  22. {
  23. inline NativeMethod(NativeScope& scope, TypeId returnType) :
  24. Method{ scope, returnType }
  25. {
  26. }
  27. virtual llvm::Value* generateCode(llvm::IRBuilder<>& builder,
  28. std::vector<llvm::Value*> arguments) = 0;
  29. };
  30. struct qlow::sem::UnaryNativeMethod : public sem::NativeMethod
  31. {
  32. std::function<llvm::Value*(llvm::IRBuilder<>&, llvm::Value*)> generator;
  33. inline UnaryNativeMethod(NativeScope& scope,
  34. TypeId returnType,
  35. const std::function
  36. <llvm::Value*(llvm::IRBuilder<>&, llvm::Value*)>& generator) :
  37. NativeMethod{ scope, returnType },
  38. generator{ generator }
  39. {
  40. }
  41. virtual llvm::Value* generateCode(llvm::IRBuilder<>& builder,
  42. std::vector<llvm::Value*> arguments);
  43. };
  44. struct qlow::sem::BinaryNativeMethod : public sem::NativeMethod
  45. {
  46. using Func =
  47. std::function<llvm::Value*(llvm::IRBuilder<>&, llvm::Value*, llvm::Value*)>;
  48. Func generator;
  49. Variable argument;
  50. inline BinaryNativeMethod(NativeScope& scope,
  51. TypeId returnType,
  52. TypeId argumentType,
  53. Func&& generator) :
  54. NativeMethod{ scope, returnType },
  55. generator{ generator },
  56. argument{ context, argumentType, "arg" }
  57. {
  58. Method::arguments = { &argument };
  59. }
  60. virtual llvm::Value* generateCode(llvm::IRBuilder<>& builder,
  61. std::vector<llvm::Value*> arguments);
  62. };
  63. #endif // QLOW_SEM_BUILTIN_H