+ Fluid location (Lily::f_location);
+
+ // (car (ly:music-signature self_scm())) is the return type, skip it
+ SCM signature = scm_cdr (get_signature ());
+
+ // The main loop just processes the signature in sequence, and the
+ // resulting actual arguments are accumulated in reverse order in args
+
+ SCM args = SCM_EOL;
+
+ while (scm_is_pair (rest) && scm_is_pair (signature))
+ {
+ SCM arg = scm_car (rest);
+ SCM pred = scm_car (signature);
+ if (!scm_is_pair (pred))
+ {
+ // non-optional argument
+ if (scm_is_false (scm_call_1 (pred, arg)))
+ {
+ Syntax::argument_error (scm_oneplus (scm_length (args)),
+ pred, arg);
+ SCM val = scm_car (get_signature ());
+ val = scm_is_pair (val) ? scm_cdr (val) : SCM_BOOL_F;
+ return with_loc (val, location);
+ }
+ }
+ // If the predicate is not a function but a pair, it
+ // signifies an optional argument. This is not quite the
+ // form declared to define-music-function (which is always
+ // a proper list) but a pair of predicate function and
+ // default value.
+ //
+ // Fall through to default argument processing when optional
+ // argument predicate matches
+ else if (scm_is_false (scm_call_1 (scm_car (pred), arg)))
+ {
+ // optional argument, non-match
+ // *unspecified* is the same as an explicit \default: skip it
+ if (scm_is_eq (arg, SCM_UNSPECIFIED))
+ rest = scm_cdr (rest);
+ // Replace this and all following optional arguments with
+ // their defaults:
+ do {
+ args = scm_cons (with_loc (scm_cdr (pred), location), args);
+ signature = scm_cdr (signature);
+ if (!scm_is_pair (signature))
+ break;
+ pred = scm_car (signature);
+ } while (scm_is_pair (pred));
+ continue;
+ }
+ // Normal processing of accepted argument
+ signature = scm_cdr (signature);
+ args = scm_cons (arg, args);
+ rest = scm_cdr (rest);
+ }
+
+ if (scm_is_pair (rest) || scm_is_pair (signature))
+ scm_wrong_num_args (self_scm ());
+
+ SCM res = scm_apply_0 (get_function (), scm_reverse_x (args, SCM_EOL));
+
+ SCM pred = scm_car (get_signature ());
+ // The return type predicate may have the form of a pair in which
+ // the car is the actual predicate and the cdr is the surrogate
+ // return value in the error case, to be extracted by
+ // music-function-call-error.
+ if (scm_is_pair (pred))
+ pred = scm_car (pred);
+
+ if (scm_is_true (scm_call_1 (pred, res)))
+ return with_loc (res, location, false);
+
+ return Syntax::music_function_call_error (self_scm (), res);