1 /*
2  * Copyright 2006-2008 The FLWOR Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #pragma once
17 #ifndef ZORBA_FUNCTIONS_FUNCTION_IMPL_H
18 #define ZORBA_FUNCTIONS_FUNCTION_IMPL_H
19 
20 #include "system/globalenv.h"
21 
22 #include "functions/function.h"
23 #include "functions/library.h"
24 
25 #include "context/static_context.h"
26 
27 #include "store/api/store.h"
28 #include "store/api/item_factory.h"
29 
30 #ifndef NDEBUG
31 
32 #include "system/properties.h"
33 
34 #define DEBUG_FN_DECL(fname, cnt)                                     \
35   if (Properties::instance()->dumpLib())                              \
36     std::cout << "Bound function " << fname->getStringValue() << "/" << cnt << std::endl;
37 
38 #else
39 
40 #define DEBUG_FN_DECL( fname, cnt ) (void)fname
41 
42 #endif
43 
44 #define DECL_WITH_KIND(sctx, type, sig, kind)                           \
45   do                                                                    \
46   {                                                                     \
47     function_t type##_ptr(new type(signature sig, kind));               \
48     const store::Item* fname = type##_ptr->getName();                   \
49     ulong cnt = type##_ptr->getSignature().paramCount();                \
50     DEBUG_FN_DECL(fname, cnt);                                          \
51     sctx->bind_fn(type##_ptr, cnt, QueryLoc::null);                     \
52     BuiltinFunctionLibrary::theFunctions[type##_ptr->getKind()] = type##_ptr.getp(); \
53   } while(0)
54 
55 #define DECL(sctx, type, sig)                                           \
56   do                                                                    \
57   {                                                                     \
58     function_t type##_ptr(new type(signature sig));                     \
59     const store::Item* fname = type##_ptr->getName();                   \
60     ulong cnt = type##_ptr->getSignature().paramCount();                \
61     DEBUG_FN_DECL(fname, cnt);                                          \
62     sctx->bind_fn(type##_ptr, cnt, QueryLoc::null);                     \
63     BuiltinFunctionLibrary::theFunctions[type##_ptr->getKind()] = type##_ptr.getp(); \
64   } while(0)
65 
66 
67 #define CODEGEN_DECL()                                    \
68 PlanIter_t codegen(CompilerCB* cb,                        \
69                    static_context* sctx,                  \
70                    const QueryLoc& loc,                   \
71                    std::vector<PlanIter_t>& argv,         \
72                    expr& ann) const;                      \
73 
74 
75 #define CODEGEN_DEF(class)                                \
76 PlanIter_t class::codegen(CompilerCB* aCb,                \
77                           static_context* aSctx,          \
78                           const QueryLoc& aLoc,           \
79                           std::vector<PlanIter_t>& aArgs, \
80                           expr& aAnn) const
81 
82 
83 #define DEFAULT_NARY_CODEGEN(Iter)                        \
84 PlanIter_t codegen(CompilerCB* /* cb */,                  \
85                    static_context* sctx,                  \
86                    const QueryLoc& loc,                   \
87                    std::vector<PlanIter_t>& argv,         \
88                    expr&/*ann*/) const                    \
89 {                                                         \
90   return new Iter(sctx, loc, argv);                       \
91 }
92 
93 
94 #define DEFAULT_BINARY_CODEGEN(Iter)                      \
95 PlanIter_t codegen(CompilerCB* /* cb */,                  \
96                    static_context* sctx,                  \
97                    const QueryLoc& loc,                   \
98                    std::vector<PlanIter_t>& argv,         \
99                    expr&/*ann*/) const                    \
100 {                                                         \
101   return new Iter(sctx, loc, argv[0], argv[1]);           \
102 }
103 
104 
105 #define DEFAULT_UNARY_CODEGEN(Iter)                       \
106 PlanIter_t codegen(CompilerCB* /* cb */,                  \
107                    static_context* sctx,                  \
108                    const QueryLoc& loc,                   \
109                    std::vector<PlanIter_t>& argv,         \
110                    expr&/*ann*/) const                    \
111 {                                                         \
112   return new Iter(sctx, loc, argv[0]);                    \
113 }
114 
115 
116 #define DEFAULT_NOARY_CODEGEN(Iter)                       \
117 PlanIter_t codegen(CompilerCB* /* cb */,                  \
118                    static_context* sctx,                  \
119                    const QueryLoc& loc,                   \
120                    std::vector<PlanIter_t>& argv,         \
121                    expr&/*ann*/) const                    \
122 {                                                         \
123   return new Iter(sctx, loc);                             \
124 }
125 
126 
127 
128 namespace zorba
129 {
130 
createQName(const char * ns,const char * pre,const char * local)131 static inline store::Item_t createQName(
132     const char* ns,
133     const char* pre,
134     const char* local)
135 {
136   store::Item_t res;
137   GENV_ITEMFACTORY->createQName(res, ns, pre, local);
138   return res;
139 }
140 
141 
142 }	/* namespace zorba */
143 
144 #endif
145 
146 /*
147  * Local variables:
148  * mode: c++
149  * End:
150  */
151 /* vim:set et sw=2 ts=2: */
152