Qore Programming Language  0.9.16
BuiltinFunction.h
1 /* -*- mode: c++; indent-tabs-mode: nil -*- */
2 /*
3  BuiltinFunction.h
4 
5  Qore Programming Language
6 
7  Copyright (C) 2003 - 2020 Qore Technologies, s.r.o.
8 
9  Permission is hereby granted, free of charge, to any person obtaining a
10  copy of this software and associated documentation files (the "Software"),
11  to deal in the Software without restriction, including without limitation
12  the rights to use, copy, modify, merge, publish, distribute, sublicense,
13  and/or sell copies of the Software, and to permit persons to whom the
14  Software is furnished to do so, subject to the following conditions:
15 
16  The above copyright notice and this permission notice shall be included in
17  all copies or substantial portions of the Software.
18 
19  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25  DEALINGS IN THE SOFTWARE.
26 
27  Note that the Qore library is released under a choice of three open-source
28  licenses: MIT (as above), LGPL 2+, or GPL 2+; see README-LICENSE for more
29  information.
30 */
31 
32 #ifndef _QORE_BUILTINFUNCTION_H
33 
34 #define _QORE_BUILTINFUNCTION_H
35 
36 #include <vector>
37 #include <string>
38 
39 class BCList;
40 class BCEAList;
41 
42 class BuiltinSignature : public AbstractFunctionSignature {
43 public:
44  DLLLOCAL BuiltinSignature(bool extra_args, const QoreTypeInfo* n_returnTypeInfo, const type_vec_t& n_typeList, const arg_vec_t& n_defaultArgList, const name_vec_t& n_names) : AbstractFunctionSignature(n_returnTypeInfo, n_typeList, n_defaultArgList, n_names) {
45  for (unsigned i = 0; i < typeList.size(); ++i) {
46  bool hasDefaultArg = i < defaultArgList.size() && defaultArgList[i];
47  if (typeList[i]) {
48  ++num_param_types;
49  if (!hasDefaultArg)
50  ++min_param_types;
51  }
52 
53  QoreTypeInfo::concatName(typeList[i], str);
54  if (names.size() > i && !names[i].empty()) {
55  str.append(" ");
56  str.append(names[i]);
57  }
58 
59  if (hasDefaultArg)
60  addDefaultArgument(defaultArgList[i]);
61 
62  // add a comma to the signature string if it's not the last parameter
63  if (i != (typeList.size() - 1))
64  str.append(", ");
65  }
66  if (extra_args) {
67  if (!typeList.empty())
68  str.append(", ");
69  str.append("...");
70  }
71  }
72 
73  DLLLOCAL virtual ~BuiltinSignature() {
74  }
75 
76  DLLLOCAL virtual const QoreTypeInfo* parseGetReturnTypeInfo() const {
77  return returnTypeInfo;
78  }
79 
80  DLLLOCAL virtual const QoreParseTypeInfo* getParseParamTypeInfo(unsigned num) const {
81  return 0;
82  }
83 };
84 
85 // the following defines the virtual functions that are common to all builtin variants
86 #define COMMON_BUILTIN_VARIANT_FUNCTIONS DLLLOCAL virtual int64 getFunctionality() const { return functionality; } \
87  DLLLOCAL virtual AbstractFunctionSignature* getSignature() const { return const_cast<BuiltinSignature*>(&signature); } \
88  DLLLOCAL virtual const QoreTypeInfo* parseGetReturnTypeInfo() const { return signature.getReturnTypeInfo(); }
89 
90 class BuiltinFunctionVariantBase {
91 public:
92  BuiltinSignature signature;
93  // functionality bitmap for parse restrictions
94  int64 functionality;
95 
96  DLLLOCAL BuiltinFunctionVariantBase(int64 n_functionality = QDOM_DEFAULT, const QoreTypeInfo* n_returnTypeInfo = 0, const type_vec_t& n_typeList = type_vec_t(), const arg_vec_t& n_defaultArgList = arg_vec_t(), const name_vec_t& n_names = name_vec_t()) :
97  signature(n_functionality & QCF_USES_EXTRA_ARGS, n_returnTypeInfo, n_typeList, n_defaultArgList, n_names), functionality(n_functionality) {
98  //printd(0, "BuiltinFunctionVariantBase::BuiltinFunctionVariantBase() this=%p flags=%lld (%lld) functionality=%lld\n", this, flags, n_flags, functionality);
99  }
100 };
101 
102 class BuiltinFunctionValueVariant : public AbstractQoreFunctionVariant, public BuiltinFunctionVariantBase {
103 protected:
104  q_func_n_t func;
105 
106 public:
107  DLLLOCAL BuiltinFunctionValueVariant(q_func_n_t m, int64 n_flags, int64 n_functionality, const QoreTypeInfo* n_returnTypeInfo = 0, const type_vec_t& n_typeList = type_vec_t(), const arg_vec_t& n_defaultArgList = arg_vec_t(), const name_vec_t& n_names = name_vec_t()) :
108  AbstractQoreFunctionVariant(n_flags), BuiltinFunctionVariantBase(n_functionality, n_returnTypeInfo, n_typeList, n_defaultArgList, n_names), func(m) {
109  }
110 
111  // the following defines the pure virtual functions that are common to all builtin variants
112  COMMON_BUILTIN_VARIANT_FUNCTIONS
113 
114  DLLLOCAL virtual QoreValue evalFunction(const char* name, CodeEvaluationHelper& ceh, ExceptionSink* xsink) const {
115  CodeContextHelper cch(xsink, CT_BUILTIN, name);
116 
117  return func(ceh.getArgs(), ceh.getRuntimeFlags(), xsink);
118  }
119 };
120 
122 
124 class BuiltinFunctionExternalVariant : public AbstractQoreFunctionVariant, public BuiltinFunctionVariantBase {
125 protected:
126  q_external_func_t func;
127  void* ptr;
128 
129 public:
130  DLLLOCAL BuiltinFunctionExternalVariant(void* ptr, q_external_func_t m, int64 n_flags, int64 n_functionality,
131  const QoreTypeInfo* n_returnTypeInfo = nullptr, const type_vec_t& n_typeList = type_vec_t(),
132  const arg_vec_t& n_defaultArgList = arg_vec_t(), const name_vec_t& n_names = name_vec_t()) :
133  AbstractQoreFunctionVariant(n_flags), BuiltinFunctionVariantBase(n_functionality, n_returnTypeInfo,
134  n_typeList, n_defaultArgList, n_names), func(m), ptr(ptr) {
135  }
136 
137  // the following defines the pure virtual functions that are common to all builtin variants
138  COMMON_BUILTIN_VARIANT_FUNCTIONS
139 
140  DLLLOCAL virtual QoreValue evalFunction(const char* name, CodeEvaluationHelper& ceh, ExceptionSink* xsink) const {
141  CodeContextHelper cch(xsink, CT_BUILTIN, name);
142 
143  return func(ptr, ceh.getArgs(), ceh.getRuntimeFlags(), xsink);
144  }
145 };
146 
147 #endif // _QORE_BUILTIN_FUNCTION
q_external_func_t
QoreValue(* q_external_func_t)(const void *ptr, const QoreListNode *args, q_rt_flags_t flags, ExceptionSink *xsink)
the type used for builtin function signatures for external functions
Definition: common.h:319
q_func_n_t
QoreValue(* q_func_n_t)(const QoreListNode *args, q_rt_flags_t flags, ExceptionSink *xsink)
the type used for builtin function signatures
Definition: common.h:307
QoreValue
The main value class in Qore, designed to be passed by value.
Definition: QoreValue.h:262
QDOM_DEFAULT
#define QDOM_DEFAULT
the default domain (no domain)
Definition: Restrictions.h:156
int64
long long int64
64bit integer type, cannot use int64_t here since it breaks the API on some 64-bit systems due to equ...
Definition: common.h:260
arg_vec_t
std::vector< QoreValue > arg_vec_t
vector of value information for default argument lists
Definition: common.h:254
ExceptionSink
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:48
BuiltinFunctionExternalVariant
External function variable.
Definition: BuiltinFunction.h:124
type_vec_t
std::vector< const QoreTypeInfo * > type_vec_t
vector of type information for parameter lists
Definition: common.h:251
name_vec_t
std::vector< std::string > name_vec_t
vector of parameter names for parameter lists
Definition: common.h:257