2
0

common.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. #
  2. # QAPI helper library
  3. #
  4. # Copyright IBM, Corp. 2011
  5. # Copyright (c) 2013-2018 Red Hat Inc.
  6. #
  7. # Authors:
  8. # Anthony Liguori <aliguori@us.ibm.com>
  9. # Markus Armbruster <armbru@redhat.com>
  10. #
  11. # This work is licensed under the terms of the GNU GPL, version 2.
  12. # See the COPYING file in the top-level directory.
  13. import re
  14. # ENUMName -> ENUM_NAME, EnumName1 -> ENUM_NAME1
  15. # ENUM_NAME -> ENUM_NAME, ENUM_NAME1 -> ENUM_NAME1, ENUM_Name2 -> ENUM_NAME2
  16. # ENUM24_Name -> ENUM24_NAME
  17. def camel_to_upper(value):
  18. c_fun_str = c_name(value, False)
  19. if value.isupper():
  20. return c_fun_str
  21. new_name = ''
  22. length = len(c_fun_str)
  23. for i in range(length):
  24. c = c_fun_str[i]
  25. # When c is upper and no '_' appears before, do more checks
  26. if c.isupper() and (i > 0) and c_fun_str[i - 1] != '_':
  27. if i < length - 1 and c_fun_str[i + 1].islower():
  28. new_name += '_'
  29. elif c_fun_str[i - 1].isdigit():
  30. new_name += '_'
  31. new_name += c
  32. return new_name.lstrip('_').upper()
  33. def c_enum_const(type_name, const_name, prefix=None):
  34. if prefix is not None:
  35. type_name = prefix
  36. return camel_to_upper(type_name) + '_' + c_name(const_name, False).upper()
  37. c_name_trans = str.maketrans('.-', '__')
  38. # Map @name to a valid C identifier.
  39. # If @protect, avoid returning certain ticklish identifiers (like
  40. # C keywords) by prepending 'q_'.
  41. #
  42. # Used for converting 'name' from a 'name':'type' qapi definition
  43. # into a generated struct member, as well as converting type names
  44. # into substrings of a generated C function name.
  45. # '__a.b_c' -> '__a_b_c', 'x-foo' -> 'x_foo'
  46. # protect=True: 'int' -> 'q_int'; protect=False: 'int' -> 'int'
  47. def c_name(name, protect=True):
  48. # ANSI X3J11/88-090, 3.1.1
  49. c89_words = set(['auto', 'break', 'case', 'char', 'const', 'continue',
  50. 'default', 'do', 'double', 'else', 'enum', 'extern',
  51. 'float', 'for', 'goto', 'if', 'int', 'long', 'register',
  52. 'return', 'short', 'signed', 'sizeof', 'static',
  53. 'struct', 'switch', 'typedef', 'union', 'unsigned',
  54. 'void', 'volatile', 'while'])
  55. # ISO/IEC 9899:1999, 6.4.1
  56. c99_words = set(['inline', 'restrict', '_Bool', '_Complex', '_Imaginary'])
  57. # ISO/IEC 9899:2011, 6.4.1
  58. c11_words = set(['_Alignas', '_Alignof', '_Atomic', '_Generic',
  59. '_Noreturn', '_Static_assert', '_Thread_local'])
  60. # GCC http://gcc.gnu.org/onlinedocs/gcc-4.7.1/gcc/C-Extensions.html
  61. # excluding _.*
  62. gcc_words = set(['asm', 'typeof'])
  63. # C++ ISO/IEC 14882:2003 2.11
  64. cpp_words = set(['bool', 'catch', 'class', 'const_cast', 'delete',
  65. 'dynamic_cast', 'explicit', 'false', 'friend', 'mutable',
  66. 'namespace', 'new', 'operator', 'private', 'protected',
  67. 'public', 'reinterpret_cast', 'static_cast', 'template',
  68. 'this', 'throw', 'true', 'try', 'typeid', 'typename',
  69. 'using', 'virtual', 'wchar_t',
  70. # alternative representations
  71. 'and', 'and_eq', 'bitand', 'bitor', 'compl', 'not',
  72. 'not_eq', 'or', 'or_eq', 'xor', 'xor_eq'])
  73. # namespace pollution:
  74. polluted_words = set(['unix', 'errno', 'mips', 'sparc', 'i386'])
  75. name = name.translate(c_name_trans)
  76. if protect and (name in c89_words | c99_words | c11_words | gcc_words
  77. | cpp_words | polluted_words):
  78. return 'q_' + name
  79. return name
  80. eatspace = '\033EATSPACE.'
  81. pointer_suffix = ' *' + eatspace
  82. def genindent(count):
  83. ret = ''
  84. for _ in range(count):
  85. ret += ' '
  86. return ret
  87. indent_level = 0
  88. def push_indent(indent_amount=4):
  89. global indent_level
  90. indent_level += indent_amount
  91. def pop_indent(indent_amount=4):
  92. global indent_level
  93. indent_level -= indent_amount
  94. # Generate @code with @kwds interpolated.
  95. # Obey indent_level, and strip eatspace.
  96. def cgen(code, **kwds):
  97. raw = code % kwds
  98. if indent_level:
  99. indent = genindent(indent_level)
  100. # re.subn() lacks flags support before Python 2.7, use re.compile()
  101. raw = re.subn(re.compile(r'^(?!(#|$))', re.MULTILINE),
  102. indent, raw)
  103. raw = raw[0]
  104. return re.sub(re.escape(eatspace) + r' *', '', raw)
  105. def mcgen(code, **kwds):
  106. if code[0] == '\n':
  107. code = code[1:]
  108. return cgen(code, **kwds)
  109. def c_fname(filename):
  110. return re.sub(r'[^A-Za-z0-9_]', '_', filename)
  111. def guardstart(name):
  112. return mcgen('''
  113. #ifndef %(name)s
  114. #define %(name)s
  115. ''',
  116. name=c_fname(name).upper())
  117. def guardend(name):
  118. return mcgen('''
  119. #endif /* %(name)s */
  120. ''',
  121. name=c_fname(name).upper())
  122. def gen_if(ifcond):
  123. ret = ''
  124. for ifc in ifcond:
  125. ret += mcgen('''
  126. #if %(cond)s
  127. ''', cond=ifc)
  128. return ret
  129. def gen_endif(ifcond):
  130. ret = ''
  131. for ifc in reversed(ifcond):
  132. ret += mcgen('''
  133. #endif /* %(cond)s */
  134. ''', cond=ifc)
  135. return ret
  136. def build_params(arg_type, boxed, extra=None):
  137. ret = ''
  138. sep = ''
  139. if boxed:
  140. assert arg_type
  141. ret += '%s arg' % arg_type.c_param_type()
  142. sep = ', '
  143. elif arg_type:
  144. assert not arg_type.variants
  145. for memb in arg_type.members:
  146. ret += sep
  147. sep = ', '
  148. if memb.optional:
  149. ret += 'bool has_%s, ' % c_name(memb.name)
  150. ret += '%s %s' % (memb.type.c_param_type(),
  151. c_name(memb.name))
  152. if extra:
  153. ret += sep + extra
  154. return ret if ret else 'void'