obs-studio/libobs/callback/decl.c
jp9000 e9342143a7 Simplify and extend callback/signalling system
- Signals and dynamic callbacks now require declarations to be made
  before being used.  What this does is allows us to get information
  about the functions dynamically which can be relayed to the user and
  plugins for future extended usage (this should have big implications
  later for scripting in particular, hopefully).

- Reduced the number of types calldata uses from "everything I could
  think of" to simply integer, float, bool, pointer/object, string.
  Integer data is now stored as long long.  Floats are now stored as
  doubles (check em).

- Use a more consistent naming scheme for lexer error/warning macros.

- Fixed a rather nasty bug where switching to an existing scene would
  cause it to increment sourceSceneRefs, which would mean that it would
  never end up never properly removing the source when the user clicks
  removed (stayed in limbo, obs_source_remove never got called)
2014-03-01 05:54:55 -07:00

219 lines
5.4 KiB
C

/*
* Copyright (c) 2014 Hugh Bailey <obs.jim@gmail.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "../util/cf-parser.h"
#include "decl.h"
static inline void err_specifier_exists(struct cf_parser *cfp,
const char *storage)
{
cf_adderror(cfp, "'$1' specifier already exists", LEX_WARNING,
storage, NULL, NULL);
}
static inline void err_reserved_name(struct cf_parser *cfp, const char *name)
{
cf_adderror(cfp, "'$1' is a reserved name", LEX_ERROR,
name, NULL, NULL);
}
static bool is_in_out_specifier(struct cf_parser *cfp, struct strref *name,
uint32_t *type)
{
if (strref_cmp(name, "in") == 0) {
if (*type & CALL_PARAM_IN)
err_specifier_exists(cfp, "in");
*type |= CALL_PARAM_IN;
} else if (strref_cmp(name, "out") == 0) {
if (*type & CALL_PARAM_OUT)
err_specifier_exists(cfp, "out");
*type |= CALL_PARAM_OUT;
} else {
return false;
}
return true;
}
#define TYPE_OR_STORAGE "type or storage specifier"
static bool get_type(struct strref *ref, enum call_param_type *type,
bool is_return)
{
if (strref_cmp(ref, "int") == 0)
*type = CALL_PARAM_TYPE_INT;
else if (strref_cmp(ref, "float") == 0)
*type = CALL_PARAM_TYPE_FLOAT;
else if (strref_cmp(ref, "bool") == 0)
*type = CALL_PARAM_TYPE_BOOL;
else if (strref_cmp(ref, "ptr") == 0)
*type = CALL_PARAM_TYPE_PTR;
else if (strref_cmp(ref, "string") == 0)
*type = CALL_PARAM_TYPE_STRING;
else if (is_return && strref_cmp(ref, "void") == 0)
*type = CALL_PARAM_TYPE_VOID;
else
return false;
return true;
}
static bool is_reserved_name(const char *str)
{
return (strcmp(str, "int") == 0) ||
(strcmp(str, "float") == 0) ||
(strcmp(str, "bool") == 0) ||
(strcmp(str, "ptr") == 0) ||
(strcmp(str, "string") == 0) ||
(strcmp(str, "void") == 0) ||
(strcmp(str, "return") == 0);
}
static int parse_param(struct cf_parser *cfp, struct decl_info *decl)
{
struct strref ref;
int code;
struct decl_param param = {0};
/* get stprage specifiers */
code = cf_next_name_ref(cfp, &ref, TYPE_OR_STORAGE, ",");
if (code != PARSE_SUCCESS)
return code;
while (is_in_out_specifier(cfp, &ref, &param.flags)) {
code = cf_next_name_ref(cfp, &ref, TYPE_OR_STORAGE, ",");
if (code != PARSE_SUCCESS)
return code;
}
/* parameters not marked with specifers are input parameters */
if (param.flags == 0)
param.flags = CALL_PARAM_IN;
if (!get_type(&ref, &param.type, false)) {
cf_adderror_expecting(cfp, "type");
cf_go_to_token(cfp, ",", ")");
return PARSE_CONTINUE;
}
/* name */
code = cf_next_name(cfp, &param.name, "parameter name", ",");
if (code != PARSE_SUCCESS)
return code;
if (is_reserved_name(param.name))
err_reserved_name(cfp, param.name);
da_push_back(decl->params, &param);
return PARSE_SUCCESS;
}
static void parse_params(struct cf_parser *cfp, struct decl_info *decl)
{
struct cf_token peek;
int code;
if (!cf_peek_valid_token(cfp, &peek))
return;
while (peek.type == CFTOKEN_NAME) {
code = parse_param(cfp, decl);
if (code == PARSE_EOF)
return;
if (code != PARSE_CONTINUE && !cf_next_valid_token(cfp))
return;
if (cf_token_is(cfp, ")"))
break;
else if (cf_token_should_be(cfp, ",", ",", NULL) == PARSE_EOF)
return;
if (!cf_peek_valid_token(cfp, &peek))
return;
}
if (!cf_token_is(cfp, ")"))
cf_next_token_should_be(cfp, ")", NULL, NULL);
}
static void print_errors(struct cf_parser *cfp, const char *decl_string)
{
char *errors = error_data_buildstring(&cfp->error_list);
if (errors) {
blog(LOG_WARNING, "Errors/warnings for '%s':\n\n%s",
decl_string, errors);
bfree(errors);
}
}
bool parse_decl_string(struct decl_info *decl, const char *decl_string)
{
struct cf_parser cfp;
struct strref ret_type;
struct decl_param ret_param = {0};
int code;
bool success;
decl->decl_string = decl_string;
ret_param.flags = CALL_PARAM_OUT;
cf_parser_init(&cfp);
if (!cf_parser_parse(&cfp, decl_string, "declaraion"))
goto fail;
code = cf_get_name_ref(&cfp, &ret_type, "return type", NULL);
if (code == PARSE_EOF)
goto fail;
if (!get_type(&ret_type, &ret_param.type, true))
cf_adderror_expecting(&cfp, "return type");
code = cf_next_name(&cfp, &decl->name, "function name", "(");
if (code == PARSE_EOF)
goto fail;
if (is_reserved_name(decl->name))
err_reserved_name(&cfp, decl->name);
code = cf_next_token_should_be(&cfp, "(", "(", NULL);
if (code == PARSE_EOF)
goto fail;
parse_params(&cfp, decl);
fail:
success = !error_data_has_errors(&cfp.error_list);
if (success && ret_param.type != CALL_PARAM_TYPE_VOID) {
ret_param.name = bstrdup("return");
da_push_back(decl->params, &ret_param);
}
if (!success)
decl_info_free(decl);
print_errors(&cfp, decl_string);
cf_parser_free(&cfp);
return success;
}