| 1 |
/* |
| 2 |
* Copyright (c) 2021-2025 Symas Corporation |
| 3 |
* |
| 4 |
* Redistribution and use in source and binary forms, with or without |
| 5 |
* modification, are permitted provided that the following conditions are |
| 6 |
* met: |
| 7 |
* |
| 8 |
* * Redistributions of source code must retain the above copyright |
| 9 |
* notice, this list of conditions and the following disclaimer. |
| 10 |
* * Redistributions in binary form must reproduce the above |
| 11 |
* copyright notice, this list of conditions and the following disclaimer |
| 12 |
* in the documentation and/or other materials provided with the |
| 13 |
* distribution. |
| 14 |
* * Neither the name of the Symas Corporation nor the names of its |
| 15 |
* contributors may be used to endorse or promote products derived from |
| 16 |
* this software without specific prior written permission. |
| 17 |
* |
| 18 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 |
*/ |
| 30 |
// This file is included only by parse.y |
| 31 |
|
| 32 |
#include <map> |
| 33 |
|
| 34 |
/* |
| 35 |
* Intrinsics |
| 36 |
* types are: |
| 37 |
* A Alphabetic |
| 38 |
* D DBCS |
| 39 |
* I Integer |
| 40 |
* K Keyword |
| 41 |
* N Numeric |
| 42 |
* O Other |
| 43 |
* U National |
| 44 |
* 8 UTF-8 |
| 45 |
* X Alphanumeric |
| 46 |
* n variadic |
| 47 |
* We use just A, I, N, or X, choosing the most general for each parameter. |
| 48 |
*/ |
| 49 |
static const function_descr_t function_descrs[] = { |
| 50 |
{ ABS, "ABS", |
| 51 |
"__gg__abs", "N", {}, FldNumericBin5 }, |
| 52 |
{ ACOS, "ACOS", |
| 53 |
"__gg__acos", "N", {}, FldNumericBin5 }, |
| 54 |
{ ANNUITY, "ANNUITY", |
| 55 |
"__gg__annuity", "NI", {}, FldNumericBin5 }, |
| 56 |
{ ASIN, "ASIN", |
| 57 |
"__gg__asin", "N", {}, FldNumericBin5 }, |
| 58 |
{ ATAN, "ATAN", |
| 59 |
"__gg__atan", "N", {}, FldNumericBin5 }, |
| 60 |
{ BASECONVERT, "BASECONVERT", |
| 61 |
"__gg__baseconvert", "XII", {}, FldNumericBin5 }, |
| 62 |
{ BIT_OF, "BIT-OF", |
| 63 |
"__gg__bit_of", "X", {}, FldAlphanumeric }, |
| 64 |
{ BIT_TO_CHAR, "BIT-TO-CHAR", |
| 65 |
"__gg__bit_to_char", "X", {}, FldAlphanumeric }, |
| 66 |
// BOOLEAN-OF-INTEGER requires FldBoolean |
| 67 |
{ BOOLEAN_OF_INTEGER, "BOOLEAN-OF-INTEGER", |
| 68 |
"__gg__boolean_of_integer", "II", {}, FldNumericBin5 }, |
| 69 |
{ BYTE_LENGTH, "BYTE-LENGTH", |
| 70 |
"__gg__byte_length", "X", {}, FldNumericBin5 }, |
| 71 |
{ CHAR, "CHAR", |
| 72 |
"__gg__char", "I", {}, FldAlphanumeric }, |
| 73 |
{ CHAR_NATIONAL, "CHAR-NATIONAL", |
| 74 |
"__gg__char_national", "I", {}, FldAlphanumeric }, |
| 75 |
{ COMBINED_DATETIME, "COMBINED-DATETIME", |
| 76 |
"__gg__combined_datetime", "IN", {}, FldNumericBin5 }, |
| 77 |
{ CONCAT, "CONCAT", |
| 78 |
"__gg__concat", "n", {}, FldAlphanumeric }, |
| 79 |
{ CONVERT, "CONVERT", |
| 80 |
"__gg__convert", "XII", {}, FldAlphanumeric }, |
| 81 |
{ COS, "COS", |
| 82 |
"__gg__cos", "N", {}, FldNumericBin5 }, |
| 83 |
{ CURRENT_DATE, "CURRENT-DATE", |
| 84 |
"__gg__current_date", "", {}, FldAlphanumeric }, |
| 85 |
{ DATE_OF_INTEGER, "DATE-OF-INTEGER", |
| 86 |
"__gg__date_of_integer", "I", {}, FldNumericBin5 }, |
| 87 |
{ DATE_TO_YYYYMMDD, "DATE-TO-YYYYMMDD", |
| 88 |
"__gg__date_to_yyyymmdd", "III", {}, FldNumericBin5 }, |
| 89 |
{ DAY_OF_INTEGER, "DAY-OF-INTEGER", |
| 90 |
"__gg__day_of_integer", "I", {}, FldNumericBin5 }, |
| 91 |
{ DAY_TO_YYYYDDD, "DAY-TO-YYYYDDD", |
| 92 |
"__gg__day_to_yyyyddd", "III", {}, FldNumericBin5 }, |
| 93 |
{ DISPLAY_OF, "DISPLAY-OF", |
| 94 |
"__gg__display_of", "UUI", {}, FldAlphanumeric }, |
| 95 |
{ E, "E", |
| 96 |
"__gg_e", "", {}, FldNumericBin5 }, |
| 97 |
|
| 98 |
{ EXCEPTION_FILE, "EXCEPTION-FILE", |
| 99 |
"__gg__func_exception_file", "", {}, FldAlphanumeric }, |
| 100 |
{ EXCEPTION_FILE_N, "EXCEPTION-FILE-N", |
| 101 |
"__gg__func_exception_file_n", "", {}, FldAlphanumeric }, |
| 102 |
{ EXCEPTION_LOCATION, "EXCEPTION-LOCATION", |
| 103 |
"__gg__func_exception_location", "", {}, FldAlphanumeric }, |
| 104 |
{ EXCEPTION_LOCATION_N, "EXCEPTION-LOCATION-N", |
| 105 |
"__gg__func_exception_location_n", "", {}, FldAlphanumeric }, |
| 106 |
{ EXCEPTION_STATEMENT, "EXCEPTION-STATEMENT", |
| 107 |
"__gg__func_exception_statement", "", {}, FldAlphanumeric }, |
| 108 |
{ EXCEPTION_STATUS, "EXCEPTION-STATUS", |
| 109 |
"__gg__func_exception_status", "", {}, FldAlphanumeric }, |
| 110 |
|
| 111 |
{ EXP, "EXP", |
| 112 |
"__gg__exp", "N", {}, FldNumericBin5 }, |
| 113 |
{ EXP10, "EXP10", |
| 114 |
"__gg__exp10", "N", {}, FldNumericBin5 }, |
| 115 |
{ FACTORIAL, "FACTORIAL", |
| 116 |
"__gg__factorial", "I", {}, FldNumericBin5 }, |
| 117 |
{ FIND_STRING, "FIND-STRING", |
| 118 |
"__gg__find_string", "AXI", {}, FldNumericBin5 }, |
| 119 |
{ FORMATTED_CURRENT_DATE, "FORMATTED-CURRENT-DATE", |
| 120 |
"__gg__formatted_current_date", "X", {}, FldAlphanumeric }, |
| 121 |
{ FORMATTED_DATE, "FORMATTED-DATE", |
| 122 |
"__gg__formatted_date", "XX", {}, FldAlphanumeric }, |
| 123 |
{ FORMATTED_DATETIME, "FORMATTED-DATETIME", |
| 124 |
"__gg__formatted_datetime", "XINI", {}, FldAlphanumeric }, |
| 125 |
{ FORMATTED_TIME, "FORMATTED-TIME", |
| 126 |
"__gg__formatted_time", "INI", {}, FldNumericBin5 }, |
| 127 |
{ FRACTION_PART, "FRACTION-PART", |
| 128 |
"__gg__fraction_part", "N", {}, FldNumericBin5 }, |
| 129 |
{ HEX_OF, "HEX-OF", |
| 130 |
"__gg__hex_of", "X", {}, FldAlphanumeric }, |
| 131 |
{ HEX_TO_CHAR, "HEX-TO-CHAR", |
| 132 |
"__gg__hex_to_char", "X", {}, FldAlphanumeric }, |
| 133 |
{ HIGHEST_ALGEBRAIC, "HIGHEST-ALGEBRAIC", |
| 134 |
"__gg__highest_algebraic", "N", {}, FldNumericBin5 }, |
| 135 |
{ INTEGER, "INTEGER", |
| 136 |
"__gg__integer", "N", {}, FldNumericBin5 }, |
| 137 |
// requires FldBoolean |
| 138 |
{ INTEGER_OF_BOOLEAN, "INTEGER-OF-BOOLEAN", |
| 139 |
"__gg__integer_of_boolean", "B", {}, FldNumericBin5 }, |
| 140 |
{ INTEGER_OF_DATE, "INTEGER-OF-DATE", |
| 141 |
"__gg__integer_of_date", "I", {}, FldNumericBin5 }, |
| 142 |
{ INTEGER_OF_DAY, "INTEGER-OF-DAY", |
| 143 |
"__gg__integer_of_day", "I", {}, FldNumericBin5 }, |
| 144 |
{ INTEGER_OF_FORMATTED_DATE, "INTEGER-OF-FORMATTED-DATE", |
| 145 |
"__gg__integer_of_formatted_date", "XX", {}, FldAlphanumeric }, |
| 146 |
{ INTEGER_PART, "INTEGER-PART", |
| 147 |
"__gg__integer_part", "N", {}, FldNumericBin5 }, |
| 148 |
{ LENGTH, "LENGTH", |
| 149 |
"__gg__length", "X", {}, FldNumericBin5 }, |
| 150 |
{ LOCALE_COMPARE, "LOCALE-COMPARE", |
| 151 |
"__gg__locale_compare", "XXX", {}, FldNumericBin5 }, |
| 152 |
{ LOCALE_DATE, "LOCALE-DATE", |
| 153 |
"__gg__locale_date", "XX", {}, FldNumericBin5 }, |
| 154 |
{ LOCALE_TIME, "LOCALE-TIME", |
| 155 |
"__gg__locale_time", "XX", {}, FldNumericBin5 }, |
| 156 |
{ LOCALE_TIME_FROM_SECONDS, "LOCALE-TIME-FROM-SECONDS", |
| 157 |
"__gg__locale_time_from_seconds", "NX", {}, FldNumericBin5 }, |
| 158 |
|
| 159 |
{ LOG, "LOG", |
| 160 |
"__gg__log", "N", {}, FldNumericBin5 }, |
| 161 |
{ LOG10, "LOG10", |
| 162 |
"__gg__log10", "N", {}, FldNumericBin5 }, |
| 163 |
{ LOWER_CASE, "LOWER-CASE", |
| 164 |
"__gg__lower_case", "X", {}, FldAlphanumeric }, |
| 165 |
{ LOWEST_ALGEBRAIC, "LOWEST-ALGEBRAIC", |
| 166 |
"__gg__lowest_algebraic", "N", {}, FldNumericBin5 }, |
| 167 |
|
| 168 |
{ MAXX, "MAX", |
| 169 |
"__gg__max", "n", {}, FldAlphanumeric }, |
| 170 |
{ MEAN, "MEAN", |
| 171 |
"__gg__mean", "n", {}, FldNumericBin5 }, |
| 172 |
{ MEDIAN, "MEDIAN", |
| 173 |
"__gg__median", "n", {}, FldNumericBin5 }, |
| 174 |
{ MIDRANGE, "MIDRANGE", |
| 175 |
"__gg__midrange", "n", {}, FldNumericBin5 }, |
| 176 |
{ MINN, "MIN", |
| 177 |
"__gg__min", "n", {}, FldAlphanumeric }, |
| 178 |
{ MOD, "MOD", |
| 179 |
"__gg__mod", "IN", {}, FldNumericBin5 }, |
| 180 |
{ MODULE_NAME, "MODULE-NAME", |
| 181 |
"__gg__module_name", "I", {}, FldAlphanumeric }, |
| 182 |
{ NATIONAL_OF, "NATIONAL-OF", |
| 183 |
"__gg__national_of", "XX", {}, FldAlphanumeric }, |
| 184 |
{ NUMVAL, "NUMVAL", |
| 185 |
"__gg__numval", "X", {}, FldNumericBin5 }, |
| 186 |
{ NUMVAL_C, "NUMVAL-C", |
| 187 |
"__gg__numval_c", "XXU", {}, FldNumericBin5 }, |
| 188 |
{ NUMVAL_F, "NUMVAL-F", |
| 189 |
"__gg__numval_f", "X", {}, FldNumericBin5 }, |
| 190 |
{ ORD, "ORD", |
| 191 |
"__gg__ord", "X", {}, FldNumericBin5 }, |
| 192 |
{ ORD_MAX, "ORD-MAX", |
| 193 |
"__gg__ord_max", "n", {}, FldNumericBin5 }, |
| 194 |
{ ORD_MIN, "ORD-MIN", |
| 195 |
"__gg__ord_min", "n", {}, FldNumericBin5 }, |
| 196 |
{ PI, "PI", |
| 197 |
"__gg__pi", "", {}, FldNumericBin5 }, |
| 198 |
{ PRESENT_VALUE, "PRESENT-VALUE", |
| 199 |
"__gg__present_value", "n", {}, FldNumericBin5 }, |
| 200 |
{ RANDOM, "RANDOM", |
| 201 |
"__gg__random", "I", {}, FldNumericBin5 }, |
| 202 |
{ RANGE, "RANGE", |
| 203 |
"__gg__range", "n", {}, FldNumericBin5 }, |
| 204 |
{ REM, "REM", |
| 205 |
"__gg__rem", "NN", {}, FldNumericBin5 }, |
| 206 |
{ REVERSE, "REVERSE", |
| 207 |
"__gg__reverse", "X", {}, FldAlphanumeric }, |
| 208 |
{ SECONDS_FROM_FORMATTED_TIME, "SECONDS-FROM-FORMATTED-TIME", |
| 209 |
"__gg__seconds_from_formatted_time", "XX", {}, FldAlphanumeric }, |
| 210 |
{ SECONDS_PAST_MIDNIGHT, "SECONDS_PAST_MIDNIGHT", |
| 211 |
"__gg__seconds_past_midnight", "", {}, FldAlphanumeric }, |
| 212 |
{ SIGN, "SIGN", |
| 213 |
"__gg__sign", "N", {}, FldNumericBin5 }, |
| 214 |
{ SIN, "SIN", |
| 215 |
"__gg__sin", "N", {}, FldNumericBin5 }, |
| 216 |
{ SMALLEST_ALGEBRAIC, "SMALLEST-ALGEBRAIC", |
| 217 |
"__gg__smallest_algebraic", "N", {}, FldNumericBin5 }, |
| 218 |
{ SQRT, "SQRT", |
| 219 |
"__gg__sqrt", "N", {}, FldNumericBin5 }, |
| 220 |
{ STANDARD_COMPARE, "STANDARD-COMPARE", |
| 221 |
"__gg__standard_compare", "XXXI", {}, FldAlphanumeric }, |
| 222 |
{ STANDARD_DEVIATION, "STANDARD-DEVIATION", |
| 223 |
"__gg__standard_deviation", "n", {}, FldNumericBin5 }, |
| 224 |
{ SUBSTITUTE, "SUBSTITUTE", |
| 225 |
"__gg__substitute", "XXX", {}, FldAlphanumeric }, |
| 226 |
{ SUM, "SUM", |
| 227 |
"__gg__sum", "n", {}, FldNumericBin5 }, |
| 228 |
{ TAN, "TAN", |
| 229 |
"__gg__tan", "N", {}, FldNumericBin5 }, |
| 230 |
{ TEST_DATE_YYYYMMDD, "TEST-DATE-YYYYMMDD", |
| 231 |
"__gg__test_date_yyyymmdd", "I", {}, FldNumericBin5 }, |
| 232 |
{ TEST_DAY_YYYYDDD, "TEST-DAY-YYYYDDD", |
| 233 |
"__gg__test_day_yyyyddd", "I", {}, FldNumericBin5 }, |
| 234 |
{ TEST_FORMATTED_DATETIME, "TEST-FORMATTED-DATETIME", |
| 235 |
"__gg__test_formatted_datetime", "XX", {}, FldNumericBin5 }, |
| 236 |
{ TEST_NUMVAL, "TEST-NUMVAL", |
| 237 |
"__gg__test_numval", "X", {}, FldNumericBin5 }, |
| 238 |
{ TEST_NUMVAL_C, "TEST-NUMVAL-C", |
| 239 |
"__gg__test_numval_c", "XXU", {}, FldNumericBin5 }, |
| 240 |
{ TEST_NUMVAL_F, "TEST-NUMVAL-F", |
| 241 |
"__gg__test_numval_f", "X", {}, FldNumericBin5 }, |
| 242 |
{ TRIM, "TRIM", |
| 243 |
"__gg__trim", "XI", {}, FldNumericBin5 }, |
| 244 |
{ ULENGTH, "ULENGTH", |
| 245 |
"__gg__ulength", "X", {}, FldAlphanumeric }, |
| 246 |
{ UPOS, "UPOS", |
| 247 |
"__gg__upos", "XI", {}, FldAlphanumeric }, |
| 248 |
{ UPPER_CASE, "UPPER-CASE", |
| 249 |
"__gg__upper_case", "X", {}, FldAlphanumeric }, |
| 250 |
{ USUBSTR, "USUBSTR", |
| 251 |
"__gg__usubstr", "XII", {}, FldAlphanumeric }, |
| 252 |
{ USUPPLEMENTARY, "USUPPLEMENTARY", |
| 253 |
"__gg__usupplementary", "X", {}, FldAlphanumeric }, |
| 254 |
{ UUID4, "UUID4", |
| 255 |
"__gg_uuid4", "", {}, FldAlphanumeric }, |
| 256 |
{ UVALID, "UVALID", |
| 257 |
"__gg__uvalid", "X", {}, FldAlphanumeric }, |
| 258 |
{ UWIDTH, "UWIDTH", |
| 259 |
"__gg__uwidth", "XI", {}, FldAlphanumeric }, |
| 260 |
{ VARIANCE, "VARIANCE", |
| 261 |
"__gg__variance", "n", {}, FldNumericBin5 }, |
| 262 |
{ WHEN_COMPILED, "WHEN-COMPILED", |
| 263 |
"__gg__when_compiled", "", {}, FldAlphanumeric }, |
| 264 |
{ YEAR_TO_YYYY, "YEAR-TO-YYYY", |
| 265 |
"__gg__year_to_yyyy", "III", {}, FldNumericBin5 }, |
| 266 |
}; |
| 267 |
|
| 268 |
static const |
| 269 |
function_descr_t *function_descrs_end = function_descrs + COUNT_OF(function_descrs); |
| 270 |
|
| 271 |
class cname_cmp { |
| 272 |
const char *cname; |
| 273 |
public: |
| 274 |
explicit cname_cmp( const char *cname ) : cname(cname) {} |
| 275 |
|
| 276 |
bool operator()( const function_descr_t& descr ) { |
| 277 |
return strlen(cname) == strlen(descr.cname) && |
| 278 |
0 == strcmp(cname, descr.cname); |
| 279 |
} |
| 280 |
bool operator()( const char that[] ) { |
| 281 |
return strlen(cname) == strlen(that) && |
| 282 |
0 == strcmp(cname, that); |
| 283 |
} |
| 284 |
}; |
| 285 |
|
| 286 |
static int |
| 287 |
intrinsic_token_of( const char name[] ) { |
| 288 |
auto pdescr = std::find_if( function_descrs, function_descrs_end, |
| 289 |
[name]( const function_descr_t& descr ) { |
| 290 |
return 0 == strcmp(name, descr.name); |
| 291 |
} ); |
| 292 |
return pdescr == function_descrs_end? 0 : pdescr->token; |
| 293 |
} |
| 294 |
|
| 295 |
/* |
| 296 |
* For variadic intrinsic functions, ensure all parameters are commensurate. |
| 297 |
* Return pointer in 1st inconsistent parameter type. |
| 298 |
* Return NULL to indicate success. |
| 299 |
*/ |
| 300 |
static cbl_refer_t * |
| 301 |
intrinsic_inconsistent_parameter( size_t n, cbl_refer_t *args ) { |
| 302 |
class commensurate_type { |
| 303 |
cbl_refer_t first; |
| 304 |
public: |
| 305 |
explicit commensurate_type( const cbl_refer_t& first ) : first(first) {} |
| 306 |
bool operator()( const cbl_refer_t& arg ) const { |
| 307 |
return is_numeric(first.field) == is_numeric(arg.field); |
| 308 |
} |
| 309 |
}; |
| 310 |
|
| 311 |
auto p = std::find_if_not(args, args + n, commensurate_type(args[0])); |
| 312 |
return p == args + n? NULL : p; |
| 313 |
} |
| 314 |
|
| 315 |
static cbl_field_type_t |
| 316 |
intrinsic_return_type( int token ) { |
| 317 |
auto p = std::find_if( function_descrs, |
| 318 |
function_descrs_end, |
| 319 |
[token]( const auto& descr ) { |
| 320 |
return token == descr.token; |
| 321 |
} ); |
| 322 |
return p == function_descrs_end? FldAlphanumeric : p->ret_type; |
| 323 |
} |
| 324 |
|
| 325 |
static const char * |
| 326 |
intrinsic_cname( int token ) { |
| 327 |
auto p = std::find_if( function_descrs, |
| 328 |
function_descrs_end, |
| 329 |
[token]( const auto& descr ) { |
| 330 |
return token == descr.token; |
| 331 |
} ); |
| 332 |
return p == function_descrs_end? NULL : p->cname; |
| 333 |
} |
| 334 |
|
| 335 |
const char * |
| 336 |
intrinsic_function_name( int token ) { |
| 337 |
auto p = std::find_if( function_descrs, |
| 338 |
function_descrs_end, |
| 339 |
[token]( const auto& descr ) { |
| 340 |
return token == descr.token; |
| 341 |
} ); |
| 342 |
return p == function_descrs_end? NULL : p->name; |
| 343 |
} |
| 344 |
|
| 345 |
/* |
| 346 |
* Provide supplied function parameters. |
| 347 |
* Return index to 1st invalid parameter type. |
| 348 |
* Return N to indicate success. |
| 349 |
*/ |
| 350 |
static size_t |
| 351 |
intrinsic_invalid_parameter( int token, |
| 352 |
const std::vector<cbl_refer_t>& args ) |
| 353 |
{ |
| 354 |
auto p = std::find_if( function_descrs, |
| 355 |
function_descrs_end, |
| 356 |
[token]( const auto& descr ) { |
| 357 |
return token == descr.token; |
| 358 |
} ); |
| 359 |
if( p == function_descrs_end ) { |
| 360 |
cbl_internal_error( "%s: intrinsic function %qs not found", |
| 361 |
__func__, keyword_str(token) ); |
| 362 |
} |
| 363 |
|
| 364 |
gcc_assert(!args.empty()); |
| 365 |
gcc_assert(p < function_descrs_end); |
| 366 |
|
| 367 |
const function_descr_t& descr = *p; |
| 368 |
|
| 369 |
size_t i = 0; |
| 370 |
for( auto arg : args ) { |
| 371 |
if( arg.field == NULL ) { |
| 372 |
i++; |
| 373 |
continue; |
| 374 |
} |
| 375 |
assert(i < strlen(descr.types)); |
| 376 |
|
| 377 |
switch(descr.types[i]) { |
| 378 |
case 'A' : //Alphabetic |
| 379 |
case 'I' : //Integer |
| 380 |
case 'N' : //Numeric |
| 381 |
case 'X' : //Alphanumeric |
| 382 |
break; |
| 383 |
case 'n' : //variadic |
| 384 |
return args.size(); |
| 385 |
break; |
| 386 |
case 'D' : //DBCS |
| 387 |
case 'K' : //Keyword |
| 388 |
case 'O' : //Other |
| 389 |
case 'U' : //National |
| 390 |
case '8' : //UTF-8 |
| 391 |
default: |
| 392 |
cbl_internal_error( "%s: invalid function descr type '%c'", |
| 393 |
__func__, descr.types[i]); |
| 394 |
} |
| 395 |
|
| 396 |
static std::map<char, const char*> typenames |
| 397 |
{ |
| 398 |
{ 'A', "Alphabetic" }, |
| 399 |
{ 'I', "Integer" }, |
| 400 |
{ 'N', "Numeric" }, |
| 401 |
{ 'X', "Alphanumeric" }, |
| 402 |
}; |
| 403 |
|
| 404 |
switch( arg.field->type ) { |
| 405 |
case FldInvalid: |
| 406 |
case FldClass: |
| 407 |
case FldConditional: |
| 408 |
case FldForward: |
| 409 |
case FldIndex: |
| 410 |
yyerror("%s: field '%s' (%s) invalid for %s parameter", |
| 411 |
descr.name, |
| 412 |
arg.field->name, cbl_field_type_str(arg.field->type), |
| 413 |
typenames[descr.types[i]]); |
| 414 |
return i; |
| 415 |
break; |
| 416 |
case FldGroup: |
| 417 |
default: |
| 418 |
break; |
| 419 |
} |
| 420 |
|
| 421 |
if( is_numeric(arg.field) || is_integer_literal(arg.field)) { |
| 422 |
if( strchr("A", descr.types[i]) != NULL ) { |
| 423 |
yyerror("%s: numeric field '%s' (%s) invalid for %s parameter", |
| 424 |
descr.name, |
| 425 |
arg.field->name, cbl_field_type_str(arg.field->type), |
| 426 |
typenames[descr.types[i]]); |
| 427 |
return i; |
| 428 |
} |
| 429 |
} else { // string field |
| 430 |
if( strchr("IN", descr.types[i]) != NULL ) { |
| 431 |
if( data_category_of(arg.field) == data_alphabetic_e ) { |
| 432 |
yyerror("%s: non-numeric field '%s' (%s) invalid for %s parameter", |
| 433 |
descr.name, |
| 434 |
arg.field->name, cbl_field_type_str(arg.field->type), |
| 435 |
typenames[descr.types[i]]); |
| 436 |
return i; |
| 437 |
} |
| 438 |
} |
| 439 |
} |
| 440 |
i++; |
| 441 |
} // end loop |
| 442 |
|
| 443 |
return args.size(); |
| 444 |
} |
| 445 |
|
| 446 |
/* |
| 447 |
* Functions used by code gen |
| 448 |
*/ |
| 449 |
|
| 450 |
size_t |
| 451 |
intrinsic_parameter_count( const char cname[] ) { |
| 452 |
const function_descr_t *descr = std::find_if(function_descrs, |
| 453 |
function_descrs_end, cname_cmp(cname)); |
| 454 |
return descr == function_descrs_end || descr->types[0] == 'n'? |
| 455 |
size_t(-1) : strlen(descr->types); |
| 456 |
} |
| 457 |
|
| 458 |
#if 0 |
| 459 |
static int |
| 460 |
yyreport_syntax_error (const yypcontext_t *ctx) |
| 461 |
{ |
| 462 |
int res = 0; |
| 463 |
YYLOCATION_PRINT (stderr, yypcontext_location (ctx)); |
| 464 |
fprintf (stderr, ": syntax error"); |
| 465 |
// Report the tokens expected at this point. |
| 466 |
{ |
| 467 |
enum { TOKENMAX = 5 }; |
| 468 |
yysymbol_kind_t expected[TOKENMAX]; |
| 469 |
int n = yypcontext_expected_tokens (ctx, expected, TOKENMAX); |
| 470 |
if (n < 0) |
| 471 |
// Forward errors to yyparse. |
| 472 |
res = n; |
| 473 |
else |
| 474 |
for (int i = 0; i < n; ++i) |
| 475 |
fprintf (stderr, "%s %s", |
| 476 |
i == 0 ? ": expected" : " or", yysymbol_name (expected[i])); |
| 477 |
} |
| 478 |
// Report the unexpected token. |
| 479 |
{ |
| 480 |
yysymbol_kind_t lookahead = yypcontext_token (ctx); |
| 481 |
if (lookahead != YYSYMBOL_YYEMPTY) |
| 482 |
fprintf (stderr, " before %s", yysymbol_name (lookahead)); |
| 483 |
} |
| 484 |
fprintf (stderr, "\n"); |
| 485 |
return res; |
| 486 |
} |
| 487 |
#endif |