qapi: Rename identical c_fun()/c_var() into c_name()

Now that the two functions are identical, we only need one of them,
and we might as well give it a more descriptive name.  Basically,
the function serves as the translation from a QAPI name into a
(portion of a) C identifier, without regards to whether it is a
variable or function name.

Signed-off-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Markus Armbruster <armbru@redhat.com>
This commit is contained in:
Eric Blake 2015-05-14 06:50:48 -06:00 committed by Markus Armbruster
parent 47299262de
commit 18df515ebb
5 changed files with 36 additions and 38 deletions

View File

@ -31,12 +31,13 @@ def generate_command_decl(name, args, ret_type):
for argname, argtype, optional in parse_args(args):
argtype = c_type(argtype, is_param=True)
if optional:
arglist += "bool has_%s, " % c_var(argname)
arglist += "%s %s, " % (argtype, c_var(argname))
arglist += "bool has_%s, " % c_name(argname)
arglist += "%s %s, " % (argtype, c_name(argname))
return mcgen('''
%(ret_type)s qmp_%(name)s(%(args)sError **errp);
''',
ret_type=c_type(ret_type), name=c_fun(name), args=arglist).strip()
ret_type=c_type(ret_type), name=c_name(name),
args=arglist).strip()
def gen_err_check(errvar):
if errvar:
@ -55,14 +56,14 @@ def gen_sync_call(name, args, ret_type, indent=0):
retval = "retval = "
for argname, argtype, optional in parse_args(args):
if optional:
arglist += "has_%s, " % c_var(argname)
arglist += "%s, " % (c_var(argname))
arglist += "has_%s, " % c_name(argname)
arglist += "%s, " % (c_name(argname))
push_indent(indent)
ret = mcgen('''
%(retval)sqmp_%(name)s(%(args)s&local_err);
''',
name=c_fun(name), args=arglist, retval=retval).rstrip()
name=c_name(name), args=arglist, retval=retval).rstrip()
if ret_type:
ret += "\n" + gen_err_check('local_err')
ret += "\n" + mcgen(''''
@ -76,7 +77,7 @@ def gen_sync_call(name, args, ret_type, indent=0):
def gen_marshal_output_call(name, ret_type):
if not ret_type:
return ""
return "qmp_marshal_output_%s(retval, ret, &local_err);" % c_fun(name)
return "qmp_marshal_output_%s(retval, ret, &local_err);" % c_name(name)
def gen_visitor_input_containers_decl(args, obj):
ret = ""
@ -101,17 +102,17 @@ def gen_visitor_input_vars_decl(args):
ret += mcgen('''
bool has_%(argname)s = false;
''',
argname=c_var(argname))
argname=c_name(argname))
if is_c_ptr(argtype):
ret += mcgen('''
%(argtype)s %(argname)s = NULL;
''',
argname=c_var(argname), argtype=c_type(argtype))
argname=c_name(argname), argtype=c_type(argtype))
else:
ret += mcgen('''
%(argtype)s %(argname)s = {0};
''',
argname=c_var(argname), argtype=c_type(argtype))
argname=c_name(argname), argtype=c_type(argtype))
pop_indent()
return ret.rstrip()
@ -144,17 +145,17 @@ v = qmp_input_get_visitor(mi);
ret += mcgen('''
visit_optional(v, &has_%(c_name)s, "%(name)s", %(errp)s);
''',
c_name=c_var(argname), name=argname, errp=errparg)
c_name=c_name(argname), name=argname, errp=errparg)
ret += gen_err_check(errarg)
ret += mcgen('''
if (has_%(c_name)s) {
''',
c_name=c_var(argname))
c_name=c_name(argname))
push_indent()
ret += mcgen('''
%(visitor)s(v, &%(c_name)s, "%(name)s", %(errp)s);
''',
c_name=c_var(argname), name=argname, argtype=argtype,
c_name=c_name(argname), name=argname, argtype=argtype,
visitor=type_visitor(argtype), errp=errparg)
ret += gen_err_check(errarg)
if optional:
@ -198,16 +199,16 @@ out:
qapi_dealloc_visitor_cleanup(md);
}
''',
c_ret_type=c_type(ret_type), c_name=c_fun(name),
c_ret_type=c_type(ret_type), c_name=c_name(name),
visitor=type_visitor(ret_type))
return ret
def gen_marshal_input_decl(name, args, ret_type, middle_mode):
if middle_mode:
return 'int qmp_marshal_input_%s(Monitor *mon, const QDict *qdict, QObject **ret)' % c_fun(name)
return 'int qmp_marshal_input_%s(Monitor *mon, const QDict *qdict, QObject **ret)' % c_name(name)
else:
return 'static void qmp_marshal_input_%s(QDict *args, QObject **ret, Error **errp)' % c_fun(name)
return 'static void qmp_marshal_input_%s(QDict *args, QObject **ret, Error **errp)' % c_name(name)
@ -304,7 +305,7 @@ def gen_registry(commands):
registry += mcgen('''
qmp_register_command("%(name)s", qmp_marshal_input_%(c_name)s, %(opts)s);
''',
name=cmd['command'], c_name=c_fun(cmd['command']),
name=cmd['command'], c_name=c_name(cmd['command']),
opts=options)
pop_indent()
ret = mcgen('''

View File

@ -17,17 +17,17 @@ import getopt
import errno
def _generate_event_api_name(event_name, params):
api_name = "void qapi_event_send_%s(" % c_fun(event_name).lower();
api_name = "void qapi_event_send_%s(" % c_name(event_name).lower();
l = len(api_name)
if params:
for argname, argentry, optional in parse_args(params):
if optional:
api_name += "bool has_%s,\n" % c_var(argname)
api_name += "bool has_%s,\n" % c_name(argname)
api_name += "".ljust(l)
api_name += "%s %s,\n" % (c_type(argentry, is_param=True),
c_var(argname))
c_name(argname))
api_name += "".ljust(l)
api_name += "Error **errp)"
@ -98,7 +98,7 @@ def generate_event_implement(api_name, event_name, params):
ret += mcgen("""
if (has_%(var)s) {
""",
var = c_var(argname))
var = c_name(argname))
push_indent()
if argentry == "str":
@ -113,7 +113,7 @@ def generate_event_implement(api_name, event_name, params):
}
""",
var_type = var_type,
var = c_var(argname),
var = c_name(argname),
type = type_name(argentry),
name = argname)

View File

@ -68,11 +68,11 @@ def generate_struct_fields(members):
ret += mcgen('''
bool has_%(c_name)s;
''',
c_name=c_var(argname))
c_name=c_name(argname))
ret += mcgen('''
%(c_type)s %(c_name)s;
''',
c_type=c_type(argentry), c_name=c_var(argname))
c_type=c_type(argentry), c_name=c_name(argname))
return ret
@ -184,7 +184,7 @@ const int %(name)s_qtypes[QTYPE_MAX] = {
''',
qtype = qtype,
abbrev = de_camel_case(name).upper(),
enum = c_fun(de_camel_case(key),False).upper())
enum = c_name(de_camel_case(key),False).upper())
ret += mcgen('''
};
@ -221,7 +221,7 @@ struct %(name)s
%(c_type)s %(c_name)s;
''',
c_type=c_type(typeinfo[key]),
c_name=c_fun(key))
c_name=c_name(key))
ret += mcgen('''
};

View File

@ -66,7 +66,7 @@ if (err) {
goto out;
}
''',
type=type_name(base), c_name=c_var('base'))
type=type_name(base), c_name=c_name('base'))
for argname, argentry, optional in parse_args(members):
if optional:
@ -74,13 +74,13 @@ if (err) {
visit_optional(m, &(*obj)->has_%(c_name)s, "%(name)s", &err);
if (!err && (*obj)->has_%(c_name)s) {
''',
c_name=c_var(argname), name=argname)
c_name=c_name(argname), name=argname)
push_indent()
ret += mcgen('''
visit_type_%(type)s(m, &(*obj)->%(c_name)s, "%(name)s", &err);
''',
type=type_name(argentry), c_name=c_var(argname),
type=type_name(argentry), c_name=c_name(argname),
name=argname)
if optional:
@ -222,7 +222,7 @@ void visit_type_%(name)s(Visitor *m, %(name)s **obj, const char *name, Error **e
''',
enum_full_value = enum_full_value,
c_type = type_name(members[key]),
c_name = c_fun(key))
c_name = c_name(key))
ret += mcgen('''
default:
@ -323,7 +323,7 @@ void visit_type_%(name)s(Visitor *m, %(name)s **obj, const char *name, Error **e
''',
enum_full_value = enum_full_value,
c_type=type_name(members[key]),
c_name=c_fun(key))
c_name=c_name(key))
ret += mcgen('''
default:

View File

@ -753,9 +753,9 @@ def camel_case(name):
new_name += ch.lower()
return new_name
c_var_trans = string.maketrans('.-', '__')
c_name_trans = string.maketrans('.-', '__')
def c_var(name, protect=True):
def c_name(name, protect=True):
# ANSI X3J11/88-090, 3.1.1
c89_words = set(['auto', 'break', 'case', 'char', 'const', 'continue',
'default', 'do', 'double', 'else', 'enum', 'extern', 'float',
@ -784,10 +784,7 @@ def c_var(name, protect=True):
polluted_words = set(['unix', 'errno'])
if protect and (name in c89_words | c99_words | c11_words | gcc_words | cpp_words | polluted_words):
return "q_" + name
return name.translate(c_var_trans)
def c_fun(name, protect=True):
return c_var(name, protect)
return name.translate(c_name_trans)
def c_list_type(name):
return '%sList' % name
@ -945,7 +942,7 @@ def guardend(name):
# ENUM_NAME -> ENUM_NAME, ENUM_NAME1 -> ENUM_NAME1, ENUM_Name2 -> ENUM_NAME2
# ENUM24_Name -> ENUM24_NAME
def _generate_enum_string(value):
c_fun_str = c_fun(value, False)
c_fun_str = c_name(value, False)
if value.isupper():
return c_fun_str