- Auto-generate all C/C++ translation code (issue #33).

- Change index parameter types from int to size_t to make 0-based range implicit.
- Make CefPrintOptions and CefMenuInfo proper wrapper classes.
- Normalize the naming of menu-related types.
- Remove unused command_line variable from test_suite.cc.

git-svn-id: https://chromiumembedded.googlecode.com/svn/trunk@408 5089003a-bbd8-11dd-ad1f-f1f9622dbc98
This commit is contained in:
Marshall Greenblatt
2011-12-08 01:38:30 +00:00
parent b4653ce1da
commit 64e08c2918
238 changed files with 14703 additions and 4322 deletions

View File

@@ -2,6 +2,7 @@
# reserved. Use of this source code is governed by a BSD-style license that
# can be found in the LICENSE file.
from date_util import *
from file_util import *
import os
import re
@@ -147,7 +148,7 @@ def get_comment(body, name):
result.append(None)
else:
break
elif line[0:2] == '/*':
elif line[0:2] == '/*' or line[-2:] == '*/':
continue
elif line[0:2] == '//':
# keep the comment line including any leading spaces
@@ -288,7 +289,8 @@ def format_translation_includes(body):
return result
def str_to_dict(str):
""" Convert a string to a dictionary. """
""" Convert a string to a dictionary. If the same key has multiple values
the values will be stored in a list. """
dict = {}
parts = string.split(str, ',')
for part in parts:
@@ -296,21 +298,40 @@ def str_to_dict(str):
if len(part) == 0:
continue
sparts = string.split(part, '=')
if len(sparts) != 2:
if len(sparts) > 2:
raise Exception('Invalid dictionary pair format: '+part)
dict[string.strip(sparts[0])] = string.strip(sparts[1])
name = string.strip(sparts[0])
if len(sparts) == 2:
val = string.strip(sparts[1])
else:
val = True
if name in dict:
# a value with this name already exists
curval = dict[name]
if not isinstance(curval, list):
# convert the string value to a list
dict[name] = [curval]
dict[name].append(val)
else:
dict[name] = val
return dict
def dict_to_str(dict):
""" Convert a dictionary to a string. """
str = []
for name in dict.keys():
str.append(name+'='+dict[name])
if not isinstance(dict[name], list):
# currently a string value
str.append(name+'='+dict[name])
else:
# currently a list value
for val in dict[name]:
str.append(name+'='+val)
return string.join(str, ',')
# regex for matching comment-formatted attributes
_cre_attrib = '/\*--cef\(([A-Za-z0-9_ ,=]{0,})\)--\*/'
_cre_attrib = '/\*--cef\(([A-Za-z0-9_ ,=:\n]{0,})\)--\*/'
# regex for matching class and function names
_cre_cfname = '([A-Za-z0-9_]{1,})'
# regex for matching typedef values and function return values
@@ -377,6 +398,25 @@ def get_next_function_impl(existing, name):
break
return result
def get_copyright():
result = \
"""// Copyright (c) $YEAR$ The Chromium Embedded Framework Authors. All rights
// reserved. Use of this source code is governed by a BSD-style license that
// can be found in the LICENSE file.
//
// ---------------------------------------------------------------------------
//
// This file was generated by the CEF translator tool. If making changes by
// hand only do so within the body of existing method and function
// implementations. See the translator.README.txt file in the tools directory
// for more information.
//
"""
# add the copyright year
return result.replace('$YEAR$', get_year())
class obj_header:
""" Class representing a C++ header file. """
@@ -632,8 +672,34 @@ class obj_class:
return self.comment
def get_attribs(self):
""" Return the class attributes as a dictionary. """
return self.attribs;
""" Return all attributes as a dictionary. """
return self.attribs
def has_attrib(self, name):
""" Return true if the specified attribute exists. """
return name in self.attribs
def get_attrib(self, name):
""" Return the first or only value for specified attribute. """
if name in self.attribs:
if isinstance(self.attribs[name], list):
# the value is a list
return self.attribs[name][0]
else:
# the value is a string
return self.attribs[name]
return None;
def get_attrib_list(self, name):
""" Return all values for specified attribute as a list. """
if name in self.attribs:
if isinstance(self.attribs[name], list):
# the value is already a list
return self.attribs[name]
else:
# convert the value to a list
return [self.attribs[name]]
return None;
def get_typedefs(self):
""" Return the array of typedef objects. """
@@ -727,7 +793,18 @@ class obj_function:
for arg in arglist:
arg = string.strip(arg)
if len(arg) > 0:
self.arguments.append(obj_argument(self, arg))
argument = obj_argument(self, arg)
if argument.needs_attrib_count_func() and \
argument.get_attrib_count_func() is None:
raise Exception("A 'count_func' attribute is required "+ \
"for the '"+argument.get_name()+ \
"' parameter to "+self.get_qualified_name())
self.arguments.append(argument)
if self.retval.needs_attrib_default_retval() and \
self.retval.get_attrib_default_retval() is None:
raise Exception("A 'default_retval' attribute is required for "+ \
self.get_qualified_name())
def __repr__(self):
return '/* '+dict_to_str(self.attribs)+' */ '+self.get_cpp_proto()
@@ -736,6 +813,15 @@ class obj_function:
""" Return the function name. """
return self.name
def get_qualified_name(self):
""" Return the fully qualified function name. """
if isinstance(self.parent, obj_header):
# global function
return self.name
else:
# member function
return self.parent.get_name()+'::'+self.name
def get_capi_name(self, prefix = None):
""" Return the CAPI function name. """
if 'capi_name' in self.attribs:
@@ -747,9 +833,35 @@ class obj_function:
return self.comment
def get_attribs(self):
""" Return the function attributes as a dictionary. """
""" Return all attributes as a dictionary. """
return self.attribs
def has_attrib(self, name):
""" Return true if the specified attribute exists. """
return name in self.attribs
def get_attrib(self, name):
""" Return the first or only value for specified attribute. """
if name in self.attribs:
if isinstance(self.attribs[name], list):
# the value is a list
return self.attribs[name][0]
else:
# the value is a string
return self.attribs[name]
return None;
def get_attrib_list(self, name):
""" Return all values for specified attribute as a list. """
if name in self.attribs:
if isinstance(self.attribs[name], list):
# the value is already a list
return self.attribs[name]
else:
# convert the value to a list
return [self.attribs[name]]
return None;
def get_retval(self):
""" Return the return value object. """
return self.retval
@@ -789,19 +901,14 @@ class obj_function:
args.append(dict['value'])
elif dict['format'] == 'multi-arg':
# add an additional argument for the size of the array
type = type.get_name()
if type[-1] == 's':
type = type[:-1]
args.append('size_t '+type+'Count')
type_name = type.get_name()
if type.is_const():
# for const arrays pass the size argument by value
args.append('size_t '+type_name+'Count')
else:
# for non-const arrays pass the size argument by address
args.append('size_t* '+type_name+'Count')
args.append(dict['value'])
elif dict['format'] == 'multi-func':
# change the function to return one value of the
# required type based on an index parameter
type = type.get_name()
if type[-1] == 's':
type = type[:-1]
args.append('int '+type+'Index')
retval = dict['value']
return { 'retval' : retval, 'name' : name, 'args' : args }
@@ -825,8 +932,7 @@ class obj_function:
if isimpl and isinstance(self, obj_function_virtual):
# enumeration return values must be qualified with the class name
type = self.get_retval().get_type()
if type.is_result_struct() and not type.is_byref() \
and not type.is_byaddr():
if type.is_result_struct() and type.is_result_struct_enum():
retval = self.parent.get_name()+'::'+retval
return { 'retval' : retval, 'name' : name, 'args' : args }
@@ -841,6 +947,28 @@ class obj_function:
if isinstance(self, obj_function_virtual) and self.is_const():
result += ' const'
return result
def is_same_side(self, other_class_name):
""" Returns true if this function is on the same side (library or
client) and the specified class. """
if isinstance(self.parent, obj_class):
# this function is part of a class
this_is_library_side = self.parent.is_library_side()
header = self.parent.parent
else:
# this function is global
this_is_library_side = True
header = self.parent
if other_class_name == 'CefBase':
other_is_library_side = False
else:
other_class = header.get_class(other_class_name)
if other_class is None:
raise Exception('Unknown class: '+other_class_name)
other_is_library_side = other_class.is_library_side()
return other_is_library_side == this_is_library_side
class obj_function_static(obj_function):
@@ -904,6 +1032,10 @@ class obj_argument:
result += ' '+self.type.get_name()
return result
def get_name(self):
""" Return the name for this argument. """
return self.type.get_name()
def remove_name(self):
""" Remove and return the name value. """
name = self.type.get_name()
@@ -921,7 +1053,210 @@ class obj_argument:
name = self.type.get_type()
if not name in list:
list[name] = self.type
def needs_attrib_count_func(self):
""" Returns true if this argument requires a 'count_func' attribute. """
# A 'count_func' attribute is required for non-const non-string vector
# attribute types
return self.type.has_name() and \
self.type.is_result_vector() and \
not self.type.is_result_vector_string() and \
not self.type.is_const()
def get_attrib_count_func(self):
""" Returns the count function for this argument. """
# The 'count_func' attribute value format is name:function
if not self.parent.has_attrib('count_func'):
return None;
name = self.type.get_name();
vals = self.parent.get_attrib_list('count_func')
for val in vals:
parts = string.split(val, ':')
if len(parts) != 2:
raise Exception("Invalid 'count_func' attribute value for "+ \
this.parent.get_qualified_name()+': '+val)
if string.strip(parts[0]) == name:
return string.strip(parts[1])
return None
def needs_attrib_default_retval(self):
""" Returns true if this argument requires a 'default_retval' attribute.
"""
# A 'default_retval' attribute is required for enumeration return value
# types.
return not self.type.has_name() and \
self.type.is_result_struct() and \
self.type.is_result_struct_enum()
def get_attrib_default_retval(self):
""" Returns the defualt return value for this argument. """
return self.parent.get_attrib('default_retval')
def get_arg_type(self):
""" Returns the argument type as defined in translator.README.txt. """
if not self.type.has_name():
raise Exception('Cannot be called for retval types')
# simple or enumeration type
if (self.type.is_result_simple() and \
self.type.get_type() != 'bool') or \
(self.type.is_result_struct() and \
self.type.is_result_struct_enum()):
if self.type.is_byref():
if self.type.is_const():
return 'simple_byref_const'
return 'simple_byref'
elif self.type.is_byaddr():
return 'simple_byaddr'
return 'simple_byval'
# boolean type
if self.type.get_type() == 'bool':
if self.type.is_byref():
return 'bool_byref'
elif self.type.is_byaddr():
return 'bool_byaddr'
return 'bool_byval'
# structure type
if self.type.is_result_struct() and self.type.is_byref():
if self.type.is_const():
return 'struct_byref_const'
return 'struct_byref'
# string type
if self.type.is_result_string() and self.type.is_byref():
if self.type.is_const():
return 'string_byref_const'
return 'string_byref'
# refptr type
if self.type.is_result_refptr():
same_side = self.parent.is_same_side(self.type.get_refptr_type())
if self.type.is_byref():
if same_side:
return 'refptr_same_byref'
return 'refptr_diff_byref'
if same_side:
return 'refptr_same'
return 'refptr_diff'
if self.type.is_result_vector():
# all vector types must be passed by reference
if not self.type.is_byref():
return 'invalid'
if self.type.is_result_vector_string():
# string vector type
if self.type.is_const():
return 'string_vec_byref_const'
return 'string_vec_byref'
if self.type.is_result_vector_simple():
if self.type.get_vector_type() != 'bool':
# simple/enumeration vector types
if self.type.is_const():
return 'simple_vec_byref_const'
return 'simple_vec_byref'
# boolean vector types
if self.type.is_const():
return 'bool_vec_byref_const'
return 'bool_vec_byref'
if self.type.is_result_vector_refptr():
# refptr vector types
same_side = self.parent.is_same_side(self.type.get_refptr_type())
if self.type.is_const():
if same_side:
return 'refptr_vec_same_byref_const'
return 'refptr_vec_diff_byref_const'
if same_side:
return 'refptr_vec_same_byref'
return 'refptr_vec_diff_byref'
# string single map type
if self.type.is_result_map_single():
if not self.type.is_byref():
return 'invalid'
if self.type.is_const():
return 'string_map_single_byref_const'
return 'string_map_single_byref'
# string multi map type
if self.type.is_result_map_multi():
if not self.type.is_byref():
return 'invalid'
if self.type.is_const():
return 'string_map_multi_byref_const'
return 'string_map_multi_byref'
return 'invalid';
def get_retval_type(self):
""" Returns the retval type as defined in translator.README.txt. """
if self.type.has_name():
raise Exception('Cannot be called for argument types')
# unsupported modifiers
if self.type.is_const() or self.type.is_byref() or \
self.type.is_byaddr():
return 'invalid'
# void types don't have a return value
if self.type.get_type() == 'void':
return 'none'
if (self.type.is_result_simple() and \
self.type.get_type() != 'bool') or \
(self.type.is_result_struct() and self.type.is_result_struct_enum()):
return 'simple'
if self.type.get_type() == 'bool':
return 'bool'
if self.type.is_result_string():
return 'string'
if self.type.is_result_refptr():
if self.parent.is_same_side(self.type.get_refptr_type()):
return 'refptr_same'
else:
return 'refptr_diff'
return 'invalid';
def get_retval_default(self, for_capi):
""" Returns the default return value based on the retval type. """
# start with the default retval attribute, if any.
retval = self.get_attrib_default_retval()
if not retval is None:
if for_capi:
# apply any appropriate C API translations.
if retval == 'true':
return '1'
if retval == 'false':
return '0'
return retval;
# next look at the retval type value.
type = self.get_retval_type()
if type == 'simple':
return self.get_type().get_result_simple_default()
elif type == 'bool':
if for_capi:
return '0'
return 'false'
elif type == 'string':
if for_capi:
return 'NULL'
return 'CefString()'
elif type == 'refptr_same' or type == 'refptr_diff':
return 'NULL'
return ''
class obj_analysis:
""" Class representing an analysis of a data type value. """
@@ -930,6 +1265,8 @@ class obj_analysis:
self.value = value
self.result_type = 'unknown'
self.result_value = None
self.result_default = None
self.refptr_type = None
# parse the argument string
partlist = string.split(string.strip(value))
@@ -996,10 +1333,11 @@ class obj_analysis:
# check for vectors
if value.find('std::vector') == 0:
self.result_type = 'vector'
val = value[12:-1]
val = string.strip(value[12:-1])
self.result_value = [
self._get_basic(val)
]
self.result_value[0]['vector_type'] = val
return True
# check for maps
@@ -1029,6 +1367,10 @@ class obj_analysis:
if not basic is None:
self.result_type = basic['result_type']
self.result_value = basic['result_value']
if 'refptr_type' in basic:
self.refptr_type = basic['refptr_type']
if 'result_default' in basic:
self.result_default = basic['result_default']
return True
return False
@@ -1043,27 +1385,28 @@ class obj_analysis:
# check for simple direct translations
simpletypes = {
'void' : 'void',
'int' : 'int',
'int64' : 'int64',
'uint64' : 'uint64',
'double' : 'double',
'long' : 'long',
'unsigned long' : 'unsigned long',
'size_t' : 'size_t',
'time_t' : 'time_t',
'bool' : 'int',
'char* const': 'char* const',
'CefCursorHandle' : 'cef_cursor_handle_t',
'CefWindowHandle' : 'cef_window_handle_t',
'CefRect' : 'cef_rect_t',
'CefThreadId' : 'cef_thread_id_t',
'CefTime' : 'cef_time_t',
'void' : ['void', ''],
'int' : ['int', '0'],
'int64' : ['int64', '0'],
'uint64' : ['uint64', '0'],
'double' : ['double', '0'],
'long' : ['long', '0'],
'unsigned long' : ['unsigned long', '0'],
'size_t' : ['size_t', '0'],
'time_t' : ['time_t', '0'],
'bool' : ['int', '0'],
'char* const': ['char* const', 'NULL'],
'CefCursorHandle' : ['cef_cursor_handle_t', 'NULL'],
'CefWindowHandle' : ['cef_window_handle_t', 'NULL'],
'CefRect' : ['cef_rect_t', 'CefRect()'],
'CefThreadId' : ['cef_thread_id_t', 'TID_UI'],
'CefTime' : ['cef_time_t', 'CefTime()'],
}
if value in simpletypes.keys():
return {
'result_type' : 'simple',
'result_value' : simpletypes[value]
'result_value' : simpletypes[value][0],
'result_default' : simpletypes[value][1],
}
# check if already a C API structure
@@ -1079,7 +1422,8 @@ class obj_analysis:
if len(list) == 1:
return {
'result_type' : 'refptr',
'result_value' : get_capi_name(list[0], True)+'*'
'result_value' : get_capi_name(list[0], True)+'*',
'refptr_type' : list[0]
}
# check for CEF structure types
@@ -1103,13 +1447,27 @@ class obj_analysis:
return self.name
def get_value(self):
""" Return the name. """
""" Return the C++ value (type + name). """
return self.value
def get_type(self):
""" Return the type. """
""" Return the C++ type. """
return self.type
def get_refptr_type(self):
""" Return the C++ class type referenced by a CefRefPtr. """
if self.is_result_vector() and self.is_result_vector_refptr():
# return the vector RefPtr type
return self.result_value[0]['refptr_type']
# return the basic RefPtr type
return self.refptr_type
def get_vector_type(self):
""" Return the C++ class type referenced by a std::vector. """
if self.is_result_vector():
return self.result_value[0]['vector_type']
return None
def is_const(self):
""" Returns true if the argument value is constant. """
return self.isconst
@@ -1126,6 +1484,10 @@ class obj_analysis:
""" Returns true if this is a simple argument type. """
return (self.result_type == 'simple')
def get_result_simple_type_root(self):
""" Return the simple structure or basic type name. """
return self.result_value
def get_result_simple_type(self):
""" Return the simple type. """
result = ''
@@ -1136,10 +1498,18 @@ class obj_analysis:
result += '*'
return result
def get_result_simple_default(self):
""" Return the default value fo the basic type. """
return self.result_default
def is_result_refptr(self):
""" Returns true if this is a reference pointer type. """
return (self.result_type == 'refptr')
def get_result_refptr_type_root(self):
""" Return the refptr type structure name. """
return self.result_value[:-1]
def get_result_refptr_type(self, defined_structs = []):
""" Return the refptr type. """
result = ''
@@ -1154,12 +1524,19 @@ class obj_analysis:
""" Returns true if this is a structure type. """
return (self.result_type == 'structure')
def is_result_struct_enum(self):
""" Returns true if this struct type is likely an enumeration. """
# structure values that are passed by reference or address must be
# structures and not enumerations
if not self.is_byref() and not self.is_byaddr():
return True
return False
def get_result_struct_type(self, defined_structs = []):
""" Return the structure or enumeration type. """
result = ''
# structure values that are passed by reference or address must be
# structures and not enumerations
if self.is_byref() or self.is_byaddr():
is_enum = self.is_result_struct_enum()
if not is_enum:
if self.is_const():
result += 'const '
if not self.result_value in defined_structs:
@@ -1167,7 +1544,7 @@ class obj_analysis:
else:
result += 'enum '
result += self.result_value
if self.is_byref() or self.is_byaddr():
if not is_enum:
result += '*'
return result
@@ -1192,6 +1569,22 @@ class obj_analysis:
""" Returns true if this is a vector type. """
return (self.result_type == 'vector')
def is_result_vector_string(self):
""" Returns true if this is a string vector. """
return self.result_value[0]['result_type'] == 'string'
def is_result_vector_simple(self):
""" Returns true if this is a string vector. """
return self.result_value[0]['result_type'] == 'simple'
def is_result_vector_refptr(self):
""" Returns true if this is a string vector. """
return self.result_value[0]['result_type'] == 'refptr'
def get_result_vector_type_root(self):
""" Return the vector structure or basic type name. """
return self.result_value[0]['result_value']
def get_result_vector_type(self, defined_structs = []):
""" Return the vector type. """
if not self.has_name():
@@ -1209,7 +1602,8 @@ class obj_analysis:
if type == 'simple':
str = value
if self.is_const():
str += ' const*'
str += ' const'
str += '*'
result['value'] = str
elif type == 'refptr':
str = ''
@@ -1217,25 +1611,29 @@ class obj_analysis:
str += 'struct _'
str += value
if self.is_const():
str += ' const*'
str += ' const'
str += '*'
result['value'] = str
else:
raise Exception('Unsupported vector type: '+type)
if self.is_const():
# const vector values must be passed as the value array parameter
# and a size parameter
result['format'] = 'multi-arg'
else:
# non-const vector values must be passed as one function to get the
# size and another function to get the element at a specified index
result['format'] = 'multi-func'
# vector values must be passed as a value array parameter
# and a size parameter
result['format'] = 'multi-arg'
return result
def is_result_map(self):
""" Returns true if this is a map type. """
return (self.result_type == 'map' or self.result_type == 'multimap')
def is_result_map_single(self):
""" Returns true if this is a single map type. """
return (self.result_type == 'map')
def is_result_map_multi(self):
""" Returns true if this is a multi map type. """
return (self.result_type == 'multimap')
def get_result_map_type(self, defined_structs = []):
""" Return the map type. """
if not self.has_name():
@@ -1278,7 +1676,7 @@ class obj_analysis:
format = resdict['format']
result += resdict['value']
if self.has_name() and format != 'multi-func':
if self.has_name():
result += ' '+self.get_name();
return {'format' : format, 'value' : result}

View File

@@ -3,6 +3,7 @@
# can be found in the LICENSE file.
from cef_parser import *
from date_util import *
def make_capi_global_funcs(funcs, defined_names, translate_map, indent):
result = ''
@@ -45,7 +46,7 @@ def make_capi_header(header):
# header string
result = \
"""// Copyright (c) 2011 Marshall A. Greenblatt. All rights reserved.
"""// Copyright (c) $YEAR$ Marshall A. Greenblatt. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
@@ -96,6 +97,8 @@ extern "C" {
#include "internal/cef_types.h"
"""
# add the copyright year
result = result.replace('$YEAR$', get_year())
# output global functions
result += make_capi_global_funcs(header.get_funcs(), defined_names,

View File

@@ -13,18 +13,8 @@ def make_cpptoc_header(header, clsname):
defname = string.upper(clsname[3:])
capiname = cls.get_capi_name()
result = \
"""// Copyright (c) 2011 The Chromium Embedded Framework Authors. All rights
// reserved. Use of this source code is governed by a BSD-style license that
// can be found in the LICENSE file.
//
// ---------------------------------------------------------------------------
//
// This file was generated by the CEF translator tool and should not edited
// by hand. See the translator.README.txt file in the tools directory for
// more information.
//
"""
result = get_copyright()
result += '#ifndef _'+defname+'_CPPTOC_H\n'+ \
'#define _'+defname+'_CPPTOC_H\n'

View File

@@ -11,49 +11,405 @@ def make_cpptoc_impl_proto(name, func, parts):
proto = 'CEF_EXPORT '+parts['retval']
proto += ' '+name+'('+string.join(parts['args'], ', ')+')'
return wrap_code(proto)
return proto
def make_cpptoc_impl_existing(name, func, impl, defined_names):
def make_cpptoc_function_impl_existing(name, func, impl, defined_names):
notify(name+' has manual edits')
# retrieve the C API prototype parts
parts = func.get_capi_parts(defined_names)
changes = format_translation_changes(impl, parts)
if len(changes) > 0:
notify('Changed prototype for '+name)
notify(name+' prototype changed')
return make_cpptoc_impl_proto(name, func, parts)+'{'+ \
return wrap_code(make_cpptoc_impl_proto(name, func, parts))+'{'+ \
changes+impl['body']+'\n}\n\n'
return result
def make_cpptoc_impl_new(name, func, defined_names):
notify('Added implementation for '+name)
def make_cpptoc_function_impl_new(name, func, defined_names):
# retrieve the C API prototype parts
parts = func.get_capi_parts(defined_names)
result = make_cpptoc_impl_proto(name, func, parts)+'{'
result = make_cpptoc_impl_proto(name, func, parts)+'\n{'
result += '\n // BEGIN DELETE BEFORE MODIFYING'
result += '\n // AUTO-GENERATED CONTENT'
invalid = []
result += '\n #pragma message("Warning: "__FILE__": '+name+' is not implemented")'
# retrieve the function arguments
args = func.get_arguments()
# determine the argument types
for arg in args:
if arg.get_arg_type() == 'invalid':
invalid.append(arg.get_name())
# retrieve the function return value
retval = func.get_retval()
retval_type = retval.get_retval_type()
if retval_type == 'invalid':
invalid.append('(return value)')
retval_default = ''
else:
retval_default = retval.get_retval_default(True)
if len(retval_default) > 0:
retval_default = ' '+retval_default;
if len(invalid) > 0:
notify(name+' could not be autogenerated')
# code could not be auto-generated
result += '\n // BEGIN DELETE BEFORE MODIFYING'
result += '\n // AUTO-GENERATED CONTENT'
result += '\n // COULD NOT IMPLEMENT DUE TO: '+string.join(invalid, ', ')
result += '\n #pragma message("Warning: "__FILE__": '+name+' is not implemented")'
result += '\n // END DELETE BEFORE MODIFYING'
result += '\n}\n\n'
return wrap_code(result)
result += '\n // AUTO-GENERATED CONTENT - DELETE THIS COMMENT BEFORE MODIFYING\n'
result_len = len(result)
optional = []
# parameter verification
if isinstance(func, obj_function_virtual):
result += '\n DCHECK(self);'\
'\n if (!self)'\
'\n return'+retval_default+';'
for arg in args:
arg_type = arg.get_arg_type()
arg_name = arg.get_type().get_name()
# skip optional params
optional_params = arg.parent.get_attrib_list('optional_param')
if not optional_params is None and arg_name in optional_params:
optional.append(arg_name)
continue
comment = '\n // Verify param: '+arg_name+'; type: '+arg_type
result += '\n // END DELETE BEFORE MODIFYING'
# NULL check all pointer arguments.
if len(parts['args']) > 0:
check = [];
for arg in parts['args']:
if arg.find('*') >= 0:
argname = string.split(arg)[-1];
result += '\n DCHECK('+argname+');'
check.append('!'+argname);
result += '\n if ('+string.join(check,' || ')+')'
result += '\n return;'
result += '\n}\n\n'
return result
if arg_type == 'simple_byref' or arg_type == 'simple_byref_const' or \
arg_type == 'simple_byaddr' or arg_type == 'bool_byref' or arg_type == 'bool_byaddr' or \
arg_type == 'struct_byref_const' or arg_type == 'struct_byref' or \
arg_type == 'string_byref_const' or arg_type == 'string_byref' or \
arg_type == 'refptr_same' or arg_type == 'refptr_same_byref' or \
arg_type == 'refptr_diff' or arg_type == 'refptr_diff_byref' or \
arg_type == 'string_vec_byref' or arg_type == 'string_vec_byref_const' or \
arg_type == 'string_map_single_byref' or arg_type == 'string_map_single_byref_const' or \
arg_type == 'string_map_multi_byref' or arg_type == 'string_map_multi_byref_const':
result += comment+\
'\n DCHECK('+arg_name+');'\
'\n if (!'+arg_name+')'\
'\n return'+retval_default+';'
elif arg_type == 'simple_vec_byref' or arg_type == 'bool_vec_byref' or \
arg_type == 'refptr_vec_same_byref' or arg_type == 'refptr_vec_diff_byref':
result += comment+\
'\n DCHECK('+arg_name+'Count && (*'+arg_name+'Count == 0 || '+arg_name+'));'\
'\n if (!'+arg_name+'Count || (*'+arg_name+'Count > 0 && !'+arg_name+'))'\
'\n return'+retval_default+';'
elif arg_type == 'simple_vec_byref_const' or arg_type == 'bool_vec_byref_const' or \
arg_type == 'refptr_vec_same_byref_const' or arg_type == 'refptr_vec_diff_byref_const':
result += comment+\
'\n DCHECK('+arg_name+'Count == 0 || '+arg_name+');'\
'\n if ('+arg_name+'Count > 0 && !'+arg_name+')'\
'\n return'+retval_default+';'
def make_cpptoc_impl(header, clsname, impl):
if len(optional) > 0:
result += '\n // Unverified params: '+string.join(optional,', ')
if len(result) != result_len:
result += '\n'
result_len = len(result)
# parameter translation
params = []
for arg in args:
arg_type = arg.get_arg_type()
arg_name = arg.get_type().get_name()
comment = '\n // Translate param: '+arg_name+'; type: '+arg_type
if arg_type == 'simple_byval' or arg_type == 'simple_byaddr':
params.append(arg_name)
elif arg_type == 'simple_byref' or arg_type == 'simple_byref_const':
data_type = arg.get_type().get_type()
default = arg.get_type().get_result_simple_default()
result += comment+\
'\n '+data_type+' '+arg_name+'Val = '+arg_name+'?*'+arg_name+':'+default+';'
params.append(arg_name+'Val')
elif arg_type == 'bool_byval':
params.append(arg_name+'?true:false')
elif arg_type == 'bool_byref' or arg_type == 'bool_byaddr':
result += comment+\
'\n bool '+arg_name+'Bool = ('+arg_name+' && *'+arg_name+')?true:false;'
if arg_type == 'bool_byref':
params.append(arg_name+'Bool')
else:
params.append('&'+arg_name+'Bool')
elif arg_type == 'struct_byref_const':
struct_type = arg.get_type().get_type()
result += comment+\
'\n '+struct_type+' '+arg_name+'Obj;'\
'\n if ('+arg_name+')'\
'\n '+arg_name+'Obj.Set(*'+arg_name+', false);'
params.append(arg_name+'Obj')
elif arg_type == 'struct_byref':
struct_type = arg.get_type().get_type()
result += comment+\
'\n '+struct_type+' '+arg_name+'Obj;'\
'\n if ('+arg_name+')'\
'\n '+arg_name+'Obj.AttachTo(*'+arg_name+');'
params.append(arg_name+'Obj')
elif arg_type == 'string_byref_const':
params.append('CefString('+arg_name+')')
elif arg_type == 'string_byref':
result += comment+\
'\n CefString '+arg_name+'Str('+arg_name+');'
params.append(arg_name+'Str')
elif arg_type == 'refptr_same' or arg_type == 'refptr_diff':
refptr_class = arg.get_type().get_refptr_type()
if arg_type == 'refptr_same':
params.append(refptr_class+'CppToC::Unwrap('+arg_name+')')
else:
params.append(refptr_class+'CToCpp::Wrap('+arg_name+')')
elif arg_type == 'refptr_same_byref' or arg_type == 'refptr_diff_byref':
refptr_class = arg.get_type().get_refptr_type()
if arg_type == 'refptr_same_byref':
assign = refptr_class+'CppToC::Unwrap(*'+arg_name+')'
else:
assign = refptr_class+'CToCpp::Wrap(*'+arg_name+')'
result += comment+\
'\n CefRefPtr<'+refptr_class+'> '+arg_name+'Ptr;'\
'\n if ('+arg_name+' && *'+arg_name+')'\
'\n '+arg_name+'Ptr = '+assign+';'\
'\n '+refptr_class+'* '+arg_name+'Orig = '+arg_name+'Ptr.get();'
params.append(arg_name+'Ptr')
elif arg_type == 'string_vec_byref' or arg_type == 'string_vec_byref_const':
result += comment+\
'\n std::vector<CefString> '+arg_name+'List;'\
'\n transfer_string_list_contents('+arg_name+', '+arg_name+'List);'
params.append(arg_name+'List')
elif arg_type == 'string_map_single_byref' or arg_type == 'string_map_single_byref_const':
result += comment+\
'\n std::map<CefString,CefString> '+arg_name+'Map;'\
'\n transfer_string_map_contents('+arg_name+', '+arg_name+'Map);'
params.append(arg_name+'Map')
elif arg_type == 'string_map_multi_byref' or arg_type == 'string_map_multi_byref_const':
result += comment+\
'\n std::multimap<CefString,CefString> '+arg_name+'Multimap;'\
'\n transfer_string_multimap_contents('+arg_name+', '+arg_name+'Multimap);'
params.append(arg_name+'Multimap')
elif arg_type == 'simple_vec_byref' or arg_type == 'bool_vec_byref' or \
arg_type == 'refptr_vec_same_byref' or arg_type == 'refptr_vec_diff_byref':
vec_type = arg.get_type().get_vector_type()
if arg_type == 'simple_vec_byref':
assign = arg_name+'[i]'
elif arg_type == 'bool_vec_byref':
assign = arg_name+'[i]?true:false'
elif arg_type == 'refptr_vec_same_byref':
refptr_class = arg.get_type().get_refptr_type()
assign = refptr_class+'CppToC::Unwrap('+arg_name+'[i])'
elif arg_type == 'refptr_vec_diff_byref':
refptr_class = arg.get_type().get_refptr_type()
assign = refptr_class+'CToCpp::Wrap('+arg_name+'[i])'
result += comment+\
'\n std::vector<'+vec_type+' > '+arg_name+'List;'\
'\n if ('+arg_name+'Count && *'+arg_name+'Count > 0 && '+arg_name+') {'\
'\n for (size_t i = 0; i < *'+arg_name+'Count; ++i) {'\
'\n '+arg_name+'List.push_back('+assign+');'\
'\n }'\
'\n }'
params.append(arg_name+'List')
elif arg_type == 'simple_vec_byref_const' or arg_type == 'bool_vec_byref_const' or \
arg_type == 'refptr_vec_same_byref_const' or arg_type == 'refptr_vec_diff_byref_const':
vec_type = arg.get_type().get_vector_type()
if arg_type == 'simple_vec_byref_const':
assign = arg_name+'[i]'
elif arg_type == 'bool_vec_byref_const':
assign = arg_name+'[i]?true:false'
elif arg_type == 'refptr_vec_same_byref_const':
refptr_class = arg.get_type().get_refptr_type()
assign = refptr_class+'CppToC::Unwrap('+arg_name+'[i])'
elif arg_type == 'refptr_vec_diff_byref_const':
refptr_class = arg.get_type().get_refptr_type()
assign = refptr_class+'CToCpp::Wrap('+arg_name+'[i])'
result += comment+\
'\n std::vector<'+vec_type+' > '+arg_name+'List;'\
'\n if ('+arg_name+'Count > 0) {'\
'\n for (size_t i = 0; i < '+arg_name+'Count; ++i) {'\
'\n '+arg_name+'List.push_back('+assign+');'\
'\n }'\
'\n }'
params.append(arg_name+'List')
if len(result) != result_len:
result += '\n'
result_len = len(result)
# execution
result += '\n // Execute\n '
if retval_type != 'none':
# has a return value
if retval_type == 'simple':
result += retval.get_type().get_result_simple_type()
else:
result += retval.get_type().get_type()
result += ' _retval = '
if isinstance(func.parent, obj_class):
# virtual and static class methods
if isinstance(func, obj_function_virtual):
result += func.parent.get_name()+'CppToC::Get(self)->'
else:
result += func.parent.get_name()+'::'
result += func.get_name()+'('
if len(params) > 0:
result += '\n '+string.join(params,',\n ')
result += ');\n'
result_len = len(result)
# parameter restoration
for arg in args:
arg_type = arg.get_arg_type()
arg_name = arg.get_type().get_name()
comment = '\n // Restore param: '+arg_name+'; type: '+arg_type
if arg_type == 'simple_byref':
result += comment+\
'\n if ('+arg_name+')'\
'\n *'+arg_name+' = '+arg_name+'Val;'
elif arg_type == 'bool_byref' or arg_type == 'bool_byaddr':
result += comment+\
'\n if ('+arg_name+')'\
'\n *'+arg_name+' = '+arg_name+'Bool?true:false;'
elif arg_type == 'struct_byref':
result += comment+\
'\n if ('+arg_name+')'\
'\n '+arg_name+'Obj.DetachTo(*'+arg_name+');'
elif arg_type == 'refptr_same_byref' or arg_type == 'refptr_diff_byref':
refptr_class = arg.get_type().get_refptr_type()
if arg_type == 'refptr_same_byref':
assign = refptr_class+'CppToC::Wrap('+arg_name+'Ptr)'
else:
assign = refptr_class+'CToCpp::Unwrap('+arg_name+'Ptr)'
result += comment+\
'\n if ('+arg_name+') {'\
'\n if ('+arg_name+'Ptr.get()) {'\
'\n if ('+arg_name+'Ptr.get() != '+arg_name+'Orig) {'\
'\n *'+arg_name+' = '+assign+';'\
'\n }'\
'\n } else {'\
'\n *'+arg_name+' = NULL;'\
'\n }'\
'\n }'
elif arg_type == 'string_vec_byref':
result += comment+\
'\n cef_string_list_clear('+arg_name+');'\
'\n transfer_string_list_contents('+arg_name+'List, '+arg_name+');'
elif arg_type == 'string_map_single_byref':
result += comment+\
'\n cef_string_map_clear('+arg_name+');'\
'\n transfer_string_map_contents('+arg_name+'Map, '+arg_name+');'
elif arg_type == 'string_map_multi_byref':
result += comment+\
'\n cef_string_multimap_clear('+arg_name+');'\
'\n transfer_string_multimap_contents('+arg_name+'Multimap, '+arg_name+');'
elif arg_type == 'simple_vec_byref' or arg_type == 'bool_vec_byref' or \
arg_type == 'refptr_vec_same_byref' or arg_type == 'refptr_vec_diff_byref':
if arg_type == 'simple_vec_byref' or arg_type == 'bool_vec_byref':
assign = arg_name+'List[i]'
elif arg_type == 'refptr_vec_same_byref':
refptr_class = arg.get_type().get_refptr_type()
assign = refptr_class+'CppToC::Wrap('+arg_name+'List[i])'
elif arg_type == 'refptr_vec_diff_byref':
refptr_class = arg.get_type().get_refptr_type()
assign = refptr_class+'CToCpp::Unwrap('+arg_name+'List[i])'
result += comment+\
'\n if ('+arg_name+'Count && '+arg_name+') {'\
'\n *'+arg_name+'Count = std::min('+arg_name+'List.size(), *'+arg_name+'Count);'\
'\n if (*'+arg_name+'Count > 0) {'\
'\n for (size_t i = 0; i < *'+arg_name+'Count; ++i) {'\
'\n '+arg_name+'[i] = '+assign+';'\
'\n }'\
'\n }'\
'\n }'
if len(result) != result_len:
result += '\n'
result_len = len(result)
# special handling for the global cef_shutdown function
if name == 'cef_shutdown' and isinstance(func.parent, obj_header):
classes = func.parent.get_classes()
names = []
for cls in classes:
if cls.has_attrib('no_debugct_check'):
continue;
if cls.is_library_side():
names.append(cls.get_name()+'CppToC')
else:
names.append(cls.get_name()+'CToCpp')
if len(names) > 0:
names = sorted(names)
result += '\n#ifndef NDEBUG'\
'\n // Check that all wrapper objects have been destroyed'
for name in names:
result += '\n DCHECK('+name+'::DebugObjCt == 0);';
result += '\n#endif // !NDEBUG'
if len(result) != result_len:
result += '\n'
result_len = len(result)
# return translation
if retval_type != 'none':
# has a return value
result += '\n // Return type: '+retval_type
if retval_type == 'simple' or retval_type == 'bool':
result += '\n return _retval;'
elif retval_type == 'string':
result += '\n return _retval.DetachToUserFree();'
elif retval_type == 'refptr_same':
refptr_class = retval.get_type().get_refptr_type()
result += '\n return '+refptr_class+'CppToC::Wrap(_retval);'
elif retval_type == 'refptr_diff':
refptr_class = retval.get_type().get_refptr_type()
result += '\n return '+refptr_class+'CToCpp::Unwrap(_retval);'
if len(result) != result_len:
result += '\n'
result += '}\n\n'
return wrap_code(result)
def make_cpptoc_function_impl(funcs, existing, prefixname, defined_names):
impl = ''
for func in funcs:
if not prefixname is None:
name = prefixname+'_'+func.get_capi_name()
else:
name = func.get_capi_name()
value = get_next_function_impl(existing, name)
if not value is None \
and value['body'].find('// AUTO-GENERATED CONTENT') < 0:
# an implementation exists that was not auto-generated
impl += make_cpptoc_function_impl_existing(name, func, value, defined_names)
else:
impl += make_cpptoc_function_impl_new(name, func, defined_names)
return impl
def make_cpptoc_class_impl(header, clsname, impl):
# structure names that have already been defined
defined_names = header.get_defined_structs()
@@ -62,7 +418,6 @@ def make_cpptoc_impl(header, clsname, impl):
if cls is None:
raise Exception('Class does not exist: '+clsname)
defname = string.upper(clsname[3:])
capiname = cls.get_capi_name()
prefixname = get_capi_name(clsname[3:], False)
@@ -70,19 +425,7 @@ def make_cpptoc_impl(header, clsname, impl):
existing = get_function_impls(impl, 'CEF_CALLBACK')
# generate virtual functions
virtualimpl = ''
funcs = cls.get_virtual_funcs()
for func in funcs:
name = prefixname+'_'+func.get_capi_name()
value = get_next_function_impl(existing, name)
if not value is None \
and value['body'].find('// AUTO-GEN') < 0:
# an implementation exists that was not auto-generated
virtualimpl += make_cpptoc_impl_existing(name, func, value,
defined_names)
else:
virtualimpl += make_cpptoc_impl_new(name, func, defined_names)
virtualimpl = make_cpptoc_function_impl(cls.get_virtual_funcs(), existing, prefixname, defined_names)
if len(virtualimpl) > 0:
virtualimpl = '\n// MEMBER FUNCTIONS - Body may be edited by hand.\n\n'+virtualimpl
@@ -93,19 +436,7 @@ def make_cpptoc_impl(header, clsname, impl):
existing = get_function_impls(impl, 'CEF_EXPORT')
# generate static functions
staticimpl = ''
funcs = cls.get_static_funcs()
for func in funcs:
name = func.get_capi_name()
value = get_next_function_impl(existing, name)
if not value is None \
and value['body'].find('// AUTO-GENERATED CONTENT') < 0:
# an implementation exists that was not auto-generated
staticimpl += make_cpptoc_impl_existing(name, func, value,
defined_names)
else:
staticimpl += make_cpptoc_impl_new(name, func, defined_names)
staticimpl = make_cpptoc_function_impl(cls.get_static_funcs(), existing, None, defined_names)
if len(staticimpl) > 0:
staticimpl = '\n// GLOBAL FUNCTIONS - Body may be edited by hand.\n\n'+staticimpl
@@ -116,21 +447,8 @@ def make_cpptoc_impl(header, clsname, impl):
includes = format_translation_includes(resultingimpl)
# build the final output
result = \
"""// Copyright (c) 2011 The Chromium Embedded Framework Authors. All rights
// reserved. Use of this source code is governed by a BSD-style license that
// can be found in the LICENSE file.
//
// ---------------------------------------------------------------------------
//
// A portion of this file was generated by the CEF translator tool. When
// making changes by hand only do so within the body of existing function
// implementations. See the translator.README.txt file in the tools directory
// for more information.
//
result = get_copyright()
"""
result += includes+'\n'+resultingimpl+'\n'
const = '// CONSTRUCTOR - Do not edit by hand.\n\n'+ \
@@ -151,16 +469,46 @@ def make_cpptoc_impl(header, clsname, impl):
return result
def make_cpptoc_global_impl(header, impl):
# structure names that have already been defined
defined_names = header.get_defined_structs()
# retrieve the existing global function implementations
existing = get_function_impls(impl, 'CEF_EXPORT')
# generate global functions
impl = make_cpptoc_function_impl(header.get_funcs(), existing, None, defined_names)
if len(impl) > 0:
impl = '\n// GLOBAL FUNCTIONS - Body may be edited by hand.\n\n'+impl
# determine what includes are required by identifying what translation
# classes are being used
includes = format_translation_includes(impl)
# build the final output
result = get_copyright()
result += includes+'\n'+impl
return result
def write_cpptoc_impl(header, clsname, dir, backup):
file = dir+os.sep+get_capi_name(clsname[3:], False)+'_cpptoc.cc'
if clsname is None:
# global file
file = dir
else:
# class file
file = dir+os.sep+get_capi_name(clsname[3:], False)+'_cpptoc.cc'
if path_exists(file):
oldcontents = read_file(file)
else:
oldcontents = ''
newcontents = make_cpptoc_impl(header, clsname, oldcontents)
if clsname is None:
newcontents = make_cpptoc_global_impl(header, oldcontents)
else:
newcontents = make_cpptoc_class_impl(header, clsname, oldcontents)
if newcontents != oldcontents:
if backup and oldcontents != '':
backup_file(file)

View File

@@ -13,19 +13,8 @@ def make_ctocpp_header(header, clsname):
defname = string.upper(clsname[3:])
capiname = cls.get_capi_name()
result = \
"""// Copyright (c) 2011 The Chromium Embedded Framework Authors. All rights
// reserved. Use of this source code is governed by a BSD-style license that
// can be found in the LICENSE file.
//
// -------------------------------------------------------------------------
//
// This file was generated by the CEF translator tool and should not edited
// by hand. See the translator.README.txt file in the tools directory for
// more information.
//
result = get_copyright()
"""
result += '#ifndef _'+defname+'_CTOCPP_H\n'+ \
'#define _'+defname+'_CTOCPP_H\n'

View File

@@ -5,54 +5,442 @@
from cef_parser import *
def make_ctocpp_impl_proto(clsname, name, func, parts):
proto = parts['retval']+' '+clsname
const = ''
if isinstance(func, obj_function_virtual):
proto += 'CToCpp'
if func.is_const():
const = ' const'
proto += '::'+name+'('+string.join(parts['args'], ', ')+')'+const
return wrap_code(proto)
if clsname is None:
proto = 'CEF_GLOBAL '+parts['retval']+' '
else:
proto = parts['retval']+' '+clsname
if isinstance(func, obj_function_virtual):
proto += 'CToCpp'
if func.is_const():
const = ' const'
proto += '::'
proto += name+'('+string.join(parts['args'], ', ')+')'+const
return proto
def make_ctocpp_impl_existing(clsname, name, func, impl):
def make_ctocpp_function_impl_existing(clsname, name, func, impl):
notify(name+' has manual edits')
# retrieve the C++ prototype parts
parts = func.get_cpp_parts(True)
changes = format_translation_changes(impl, parts)
if len(changes) > 0:
if isinstance(func, obj_function_virtual):
notify('Changed prototype for '+clsname+'CToCpp::'+name)
else:
notify('Changed prototype for '+clsname+'::'+name)
notify(name+' prototype changed')
return make_ctocpp_impl_proto(clsname, name, func, parts)+'{'+ \
return wrap_code(make_ctocpp_impl_proto(clsname, name, func, parts))+'{'+ \
changes+impl['body']+'\n}\n\n'
def make_ctocpp_impl_new(clsname, name, func):
if isinstance(func, obj_function_virtual):
notify('Added implementation for '+clsname+'CToCpp::'+name)
else:
notify('Added implementation for '+clsname+'::'+name)
# retrieve the C++ prototype parts
def make_ctocpp_function_impl_new(clsname, name, func):
# build the C++ prototype
parts = func.get_cpp_parts(True)
result = make_ctocpp_impl_proto(clsname, name, func, parts)+'{'
result = make_ctocpp_impl_proto(clsname, name, func, parts)+'\n{'
result += '\n // BEGIN DELETE BEFORE MODIFYING'
result += '\n // AUTO-GENERATED CONTENT'
invalid = []
result += '\n #pragma message("Warning: "__FILE__": '+name+' is not implemented")'
# retrieve the function arguments
args = func.get_arguments()
# determine the argument types
for arg in args:
if arg.get_arg_type() == 'invalid':
invalid.append(arg.get_name())
# retrieve the function return value
retval = func.get_retval()
retval_type = retval.get_retval_type()
if retval_type == 'invalid':
invalid.append('(return value)')
retval_default = ''
else:
retval_default = retval.get_retval_default(False)
if len(retval_default) > 0:
retval_default = ' '+retval_default;
# add revision check
if func.has_attrib('revision_check'):
result += '\n int build_revision = cef_build_revision();'\
'\n if (build_revision != CEF_REVISION) {'\
'\n // The libcef build revision does not match the CEF API revision.'\
'\n DCHECK(false);'\
'\n return'+retval_default+';'\
'\n }\n'
if isinstance(func, obj_function_virtual):
# add the structure size check
result += '\n if (CEF_MEMBER_MISSING(struct_, '+func.get_capi_name()+'))'
result += '\n return'+retval_default+';\n'
result += '\n // END DELETE BEFORE MODIFYING'
if len(invalid) > 0:
notify(name+' could not be autogenerated')
# code could not be auto-generated
result += '\n // BEGIN DELETE BEFORE MODIFYING'
result += '\n // AUTO-GENERATED CONTENT'
result += '\n // COULD NOT IMPLEMENT DUE TO: '+string.join(invalid, ', ')
result += '\n #pragma message("Warning: "__FILE__": '+name+' is not implemented")'
result += '\n // END DELETE BEFORE MODIFYING'
result += '\n}\n\n'
return wrap_code(result)
result += '\n if (CEF_MEMBER_MISSING(struct_, '+func.get_capi_name()+'))'
result += '\n return;'
result += '\n // AUTO-GENERATED CONTENT - DELETE THIS COMMENT BEFORE MODIFYING\n'
result += '\n}\n\n'
return result
result_len = len(result)
optional = []
# parameter verification
for arg in args:
arg_type = arg.get_arg_type()
arg_name = arg.get_type().get_name()
# skip optional params
optional_params = arg.parent.get_attrib_list('optional_param')
if not optional_params is None and arg_name in optional_params:
optional.append(arg_name)
continue
comment = '\n // Verify param: '+arg_name+'; type: '+arg_type
def make_ctocpp_impl(header, clsname, impl):
if arg_type == 'simple_byaddr' or arg_type == 'bool_byaddr':
result += comment+\
'\n DCHECK('+arg_name+');'\
'\n if (!'+arg_name+')'\
'\n return'+retval_default+';'
elif arg_type == 'refptr_same' or arg_type == 'refptr_diff':
result += comment+\
'\n DCHECK('+arg_name+'.get());'\
'\n if (!'+arg_name+'.get())'\
'\n return'+retval_default+';'
elif arg_type == 'string_byref_const':
result += comment+\
'\n DCHECK(!'+arg_name+'.empty());'\
'\n if ('+arg_name+'.empty())'\
'\n return'+retval_default+';'
if len(optional) > 0:
result += '\n // Unverified params: '+string.join(optional,', ')
if len(result) != result_len:
result += '\n'
result_len = len(result)
# parameter translation
params = []
if isinstance(func, obj_function_virtual):
params.append('struct_')
for arg in args:
arg_type = arg.get_arg_type()
arg_name = arg.get_type().get_name()
comment = '\n // Translate param: '+arg_name+'; type: '+arg_type
if arg_type == 'simple_byval' or arg_type == 'simple_byaddr' or \
arg_type == 'bool_byval':
params.append(arg_name)
elif arg_type == 'simple_byref' or arg_type == 'simple_byref_const' or \
arg_type == 'struct_byref_const' or arg_type == 'struct_byref':
params.append('&'+arg_name)
elif arg_type == 'bool_byref':
result += comment+\
'\n int '+arg_name+'Int = '+arg_name+';'
params.append('&'+arg_name+'Int')
elif arg_type == 'bool_byaddr':
result += comment+\
'\n int '+arg_name+'Int = '+arg_name+'?*'+arg_name+':0;'
params.append('&'+arg_name+'Int')
elif arg_type == 'string_byref_const':
params.append(arg_name+'.GetStruct()')
elif arg_type == 'string_byref':
params.append(arg_name+'.GetWritableStruct()')
elif arg_type == 'refptr_same':
refptr_class = arg.get_type().get_refptr_type()
params.append(refptr_class+'CToCpp::Unwrap('+arg_name+')')
elif arg_type == 'refptr_diff':
refptr_class = arg.get_type().get_refptr_type()
params.append(refptr_class+'CppToC::Wrap('+arg_name+')')
elif arg_type == 'refptr_same_byref' or arg_type == 'refptr_diff_byref':
refptr_class = arg.get_type().get_refptr_type()
refptr_struct = arg.get_type().get_result_refptr_type_root()
if arg_type == 'refptr_same_byref':
assign = refptr_class+'CToCpp::Unwrap('+arg_name+')'
else:
assign = refptr_class+'CppToC::Wrap('+arg_name+')'
result += comment+\
'\n '+refptr_struct+'* '+arg_name+'Struct = NULL;'\
'\n if('+arg_name+'.get())'\
'\n '+arg_name+'Struct = '+assign+';'\
'\n '+refptr_struct+'* '+arg_name+'Orig = '+arg_name+'Struct;'
params.append('&'+arg_name+'Struct')
elif arg_type == 'string_vec_byref' or arg_type == 'string_vec_byref_const':
result += comment+\
'\n cef_string_list_t '+arg_name+'List = cef_string_list_alloc();'\
'\n DCHECK('+arg_name+'List);'\
'\n if ('+arg_name+'List)'\
'\n transfer_string_list_contents('+arg_name+', '+arg_name+'List);'
params.append(arg_name+'List')
elif arg_type == 'string_map_single_byref' or arg_type == 'string_map_single_byref_const':
result += comment+\
'\n cef_string_map_t '+arg_name+'Map = cef_string_map_alloc();'\
'\n DCHECK('+arg_name+'Map);'\
'\n if ('+arg_name+'Map)'\
'\n transfer_string_map_contents('+arg_name+', '+arg_name+'Map);'
params.append(arg_name+'Map')
elif arg_type == 'string_map_multi_byref' or arg_type == 'string_map_multi_byref_const':
result += comment+\
'\n cef_string_multimap_t '+arg_name+'Multimap = cef_string_multimap_alloc();'\
'\n DCHECK('+arg_name+'Multimap);'\
'\n if ('+arg_name+'Multimap)'\
'\n transfer_string_multimap_contents('+arg_name+', '+arg_name+'Multimap);'
params.append(arg_name+'Multimap')
elif arg_type == 'simple_vec_byref' or arg_type == 'bool_vec_byref' or \
arg_type == 'refptr_vec_same_byref' or arg_type == 'refptr_vec_diff_byref':
count_func = arg.get_attrib_count_func()
vec_type = arg.get_type().get_result_vector_type_root()
if arg_type == 'refptr_vec_same_byref':
refptr_class = arg.get_type().get_refptr_type()
assign = refptr_class+'CToCpp::Unwrap('+arg_name+'[i])'
elif arg_type == 'refptr_vec_diff_byref':
refptr_class = arg.get_type().get_refptr_type()
assign = refptr_class+'CppToC::Wrap('+arg_name+'[i])'
else:
assign = arg_name+'[i]'
result += comment+\
'\n size_t '+arg_name+'Size = '+arg_name+'.size();'\
'\n size_t '+arg_name+'Count = std::max('+count_func+'(), '+arg_name+'Size);'\
'\n '+vec_type+'* '+arg_name+'List = NULL;'\
'\n if ('+arg_name+'Count > 0) {'\
'\n '+arg_name+'List = new '+vec_type+'['+arg_name+'Count];'\
'\n DCHECK('+arg_name+'List);'\
'\n if ('+arg_name+'List) {'\
'\n memset('+arg_name+'List, 0, sizeof('+vec_type+')*'+arg_name+'Count);'\
'\n }'\
'\n if ('+arg_name+'List && '+arg_name+'Size > 0) {'\
'\n for (size_t i = 0; i < '+arg_name+'Size; ++i) {'\
'\n '+arg_name+'List[i] = '+assign+';'\
'\n }'\
'\n }'\
'\n }'
params.append('&'+arg_name+'Count')
params.append(arg_name+'List')
elif arg_type == 'simple_vec_byref_const' or arg_type == 'bool_vec_byref_const' or \
arg_type == 'refptr_vec_same_byref_const' or arg_type == 'refptr_vec_diff_byref_const':
count_func = arg.get_attrib_count_func()
vec_type = arg.get_type().get_result_vector_type_root()
if arg_type == 'refptr_vec_same_byref_const':
refptr_class = arg.get_type().get_refptr_type()
assign = refptr_class+'CToCpp::Unwrap('+arg_name+'[i])'
elif arg_type == 'refptr_vec_diff_byref_const':
refptr_class = arg.get_type().get_refptr_type()
assign = refptr_class+'CppToC::Wrap('+arg_name+'[i])'
else:
assign = arg_name+'[i]'
result += comment+\
'\n const size_t '+arg_name+'Count = '+arg_name+'.size();'\
'\n '+vec_type+'* '+arg_name+'List = NULL;'\
'\n if ('+arg_name+'Count > 0) {'\
'\n '+arg_name+'List = new '+vec_type+'['+arg_name+'Count];'\
'\n DCHECK('+arg_name+'List);'\
'\n if ('+arg_name+'List) {'\
'\n for (size_t i = 0; i < '+arg_name+'Count; ++i) {'\
'\n '+arg_name+'List[i] = '+assign+';'\
'\n }'\
'\n }'\
'\n }'
params.append(arg_name+'Count')
params.append(arg_name+'List')
if len(result) != result_len:
result += '\n'
result_len = len(result)
# execution
result += '\n // Execute\n '
if retval_type != 'none':
# has a return value
if retval_type == 'simple' or retval_type == 'bool':
result += retval.get_type().get_result_simple_type_root()
elif retval_type == 'string':
result += 'cef_string_userfree_t'
elif retval_type == 'refptr_same' or retval_type == 'refptr_diff':
refptr_struct = retval.get_type().get_result_refptr_type_root()
result += refptr_struct+'*'
result += ' _retval = '
if isinstance(func, obj_function_virtual):
result += 'struct_->'
result += func.get_capi_name()+'('
if len(params) > 0:
if not isinstance(func, obj_function_virtual):
result += '\n '
result += string.join(params,',\n ')
result += ');\n'
result_len = len(result)
# parameter restoration
for arg in args:
arg_type = arg.get_arg_type()
arg_name = arg.get_type().get_name()
comment = '\n // Restore param:'+arg_name+'; type: '+arg_type
if arg_type == 'bool_byref':
result += comment+\
'\n '+arg_name+' = '+arg_name+'Int?true:false;'
elif arg_type == 'bool_byaddr':
result += comment+\
'\n if ('+arg_name+')'\
'\n *'+arg_name+' = '+arg_name+'Int?true:false;'
elif arg_type == 'refptr_same_byref' or arg_type == 'refptr_diff_byref':
refptr_class = arg.get_type().get_refptr_type()
refptr_struct = arg.get_type().get_result_refptr_type_root()
if arg_type == 'refptr_same_byref':
assign = refptr_class+'CToCpp::Wrap('+arg_name+'Struct)'
else:
assign = refptr_class+'CppToC::Unwrap('+arg_name+'Struct)'
result += comment+\
'\n if ('+arg_name+'Struct) {'\
'\n if ('+arg_name+'Struct != '+arg_name+'Orig) {'\
'\n '+arg_name+' = '+assign+';'\
'\n }'\
'\n } else {'\
'\n '+arg_name+' = NULL;'\
'\n }'
elif arg_type == 'string_vec_byref':
result += comment+\
'\n if ('+arg_name+'List) {'\
'\n '+arg_name+'.clear();'\
'\n transfer_string_list_contents('+arg_name+'List, '+arg_name+');'\
'\n cef_string_list_free('+arg_name+'List);'\
'\n }'
elif arg_type == 'string_vec_byref_const':
result += comment+\
'\n if ('+arg_name+'List)'\
'\n cef_string_list_free('+arg_name+'List);'
elif arg_type == 'string_map_single_byref':
result += comment+\
'\n if ('+arg_name+'Map) {'\
'\n '+arg_name+'.clear();'\
'\n transfer_string_map_contents('+arg_name+'Map, '+arg_name+');'\
'\n cef_string_map_free('+arg_name+'Map);'\
'\n }'
elif arg_type == 'string_map_single_byref_const':
result += comment+\
'\n if ('+arg_name+'Map)'\
'\n cef_string_map_free('+arg_name+'Map);'
elif arg_type == 'string_map_multi_byref':
result += comment+\
'\n if ('+arg_name+'Multimap) {'\
'\n '+arg_name+'.clear();'\
'\n transfer_string_multimap_contents('+arg_name+'Multimap, '+arg_name+');'\
'\n cef_string_multimap_free('+arg_name+'Multimap);'\
'\n }'
elif arg_type == 'string_map_multi_byref_const':
result += comment+\
'\n if ('+arg_name+'Multimap)'\
'\n cef_string_multimap_free('+arg_name+'Multimap);'
elif arg_type == 'simple_vec_byref' or arg_type == 'bool_vec_byref' or \
arg_type == 'refptr_vec_same_byref' or arg_type == 'refptr_vec_diff_byref':
count_func = arg.get_attrib_count_func()
vec_type = arg.get_type().get_result_vector_type_root()
if arg_type == 'refptr_vec_same_byref':
refptr_class = arg.get_type().get_refptr_type()
assign = refptr_class+'CToCpp::Wrap('+arg_name+'List[i])'
elif arg_type == 'refptr_vec_diff_byref':
refptr_class = arg.get_type().get_refptr_type()
assign = refptr_class+'CppToC::Unwrap('+arg_name+'List[i])'
elif arg_type == 'bool_vec_byref':
assign = arg_name+'List[i]?true:false'
else:
assign = arg_name+'List[i]'
result += comment+\
'\n '+arg_name+'.clear();'\
'\n if ('+arg_name+'Count > 0 && '+arg_name+'List) {'\
'\n for (size_t i = 0; i < '+arg_name+'Count; ++i) {'\
'\n '+arg_name+'.push_back('+assign+');'\
'\n }'\
'\n delete [] '+arg_name+'List;'\
'\n }'
if len(result) != result_len:
result += '\n'
result_len = len(result)
# special handling for the global CefShutdown function
if name == 'CefShutdown' and isinstance(func.parent, obj_header):
classes = func.parent.get_classes()
names = []
for cls in classes:
if cls.has_attrib('no_debugct_check'):
continue;
if cls.is_library_side():
names.append(cls.get_name()+'CToCpp')
else:
names.append(cls.get_name()+'CppToC')
if len(names) > 0:
names = sorted(names)
result += '\n#ifndef NDEBUG'\
'\n // Check that all wrapper objects have been destroyed'
for name in names:
result += '\n DCHECK('+name+'::DebugObjCt == 0);';
result += '\n#endif // !NDEBUG'
if len(result) != result_len:
result += '\n'
result_len = len(result)
# return translation
if retval_type != 'none':
# has a return value
result += '\n // Return type: '+retval_type
if retval_type == 'simple':
result += '\n return _retval;'
elif retval_type == 'bool':
result += '\n return _retval?true:false;'
elif retval_type == 'string':
result += '\n CefString _retvalStr;'\
'\n _retvalStr.AttachToUserFree(_retval);'\
'\n return _retvalStr;'
elif retval_type == 'refptr_same':
refptr_class = retval.get_type().get_refptr_type()
result += '\n return '+refptr_class+'CToCpp::Wrap(_retval);'
elif retval_type == 'refptr_diff':
refptr_class = retval.get_type().get_refptr_type()
result += '\n return '+refptr_class+'CppToC::Unwrap(_retval);'
if len(result) != result_len:
result += '\n'
result += '}\n\n'
return wrap_code(result)
def make_ctocpp_function_impl(clsname, funcs, existing):
impl = ''
for func in funcs:
name = func.get_name()
value = get_next_function_impl(existing, name)
if not value is None \
and value['body'].find('// AUTO-GENERATED CONTENT') < 0:
# an implementation exists that was not auto-generated
impl += make_ctocpp_function_impl_existing(clsname, name, func, value)
else:
impl += make_ctocpp_function_impl_new(clsname, name, func)
return impl
def make_ctocpp_class_impl(header, clsname, impl):
cls = header.get_class(clsname)
if cls is None:
raise Exception('Class does not exist: '+clsname)
@@ -63,19 +451,7 @@ def make_ctocpp_impl(header, clsname, impl):
existing = get_function_impls(impl, clsname+'CToCpp::')
# generate virtual functions
virtualimpl = ''
funcs = cls.get_virtual_funcs()
for func in funcs:
name = func.get_name()
value = get_next_function_impl(existing, name)
if not value is None \
and value['body'].find('// AUTO-GEN') < 0:
# an implementation exists that was not auto-generated
virtualimpl += make_ctocpp_impl_existing(clsname, name, func,
value)
else:
virtualimpl += make_ctocpp_impl_new(clsname, name, func)
virtualimpl = make_ctocpp_function_impl(clsname, cls.get_virtual_funcs(), existing)
if len(virtualimpl) > 0:
virtualimpl = '\n// VIRTUAL METHODS - Body may be edited by hand.\n\n'+virtualimpl
@@ -83,19 +459,7 @@ def make_ctocpp_impl(header, clsname, impl):
existing = get_function_impls(impl, clsname+'::')
# generate static functions
staticimpl = ''
funcs = cls.get_static_funcs()
for func in funcs:
name = func.get_name()
value = get_next_function_impl(existing, name)
if not value is None \
and value['body'].find('// AUTO-GENERATED CONTENT') < 0:
# an implementation exists that was not auto-generated
staticimpl += make_ctocpp_impl_existing(clsname, name, func,
value)
else:
staticimpl += make_ctocpp_impl_new(clsname, name, func)
staticimpl = make_ctocpp_function_impl(clsname, cls.get_static_funcs(), existing)
if len(staticimpl) > 0:
staticimpl = '\n// STATIC METHODS - Body may be edited by hand.\n\n'+staticimpl
@@ -106,21 +470,8 @@ def make_ctocpp_impl(header, clsname, impl):
includes = format_translation_includes(resultingimpl)
# build the final output
result = \
"""// Copyright (c) 2011 The Chromium Embedded Framework Authors. All rights
// reserved. Use of this source code is governed by a BSD-style license that
// can be found in the LICENSE file.
//
// ---------------------------------------------------------------------------
//
// A portion of this file was generated by the CEF translator tool. When
// making changes by hand only do so within the body of existing static and
// virtual method implementations. See the translator.README.txt file in the
// tools directory for more information.
//
result = get_copyright()
"""
result += includes+'\n'+resultingimpl+'\n'
result += wrap_code('#ifndef NDEBUG\n'+ \
@@ -129,16 +480,43 @@ def make_ctocpp_impl(header, clsname, impl):
return result
def make_ctocpp_global_impl(header, impl):
# retrieve the existing global function implementations
existing = get_function_impls(impl, 'CEF_GLOBAL')
# generate static functions
impl = make_ctocpp_function_impl(None, header.get_funcs(), existing)
if len(impl) > 0:
impl = '\n// GLOBAL METHODS - Body may be edited by hand.\n\n'+impl
# determine what includes are required by identifying what translation
# classes are being used
includes = format_translation_includes(impl)
# build the final output
result = get_copyright()
result += includes+'\n// Define used to facilitate parsing.\n#define CEF_GLOBAL\n\n'+impl
return result
def write_ctocpp_impl(header, clsname, dir, backup):
file = dir+os.sep+get_capi_name(clsname[3:], False)+'_ctocpp.cc'
if clsname is None:
# global file
file = dir
else:
# class file
file = dir+os.sep+get_capi_name(clsname[3:], False)+'_ctocpp.cc'
if path_exists(file):
oldcontents = read_file(file)
else:
oldcontents = ''
newcontents = make_ctocpp_impl(header, clsname, oldcontents)
if clsname is None:
newcontents = make_ctocpp_global_impl(header, oldcontents)
else:
newcontents = make_ctocpp_class_impl(header, clsname, oldcontents)
if newcontents != oldcontents:
if backup and oldcontents != '':
backup_file(file)

File diff suppressed because it is too large Load Diff

View File

@@ -1,3 +1,3 @@
@echo off
..\..\third_party\python_26\python.exe translator.py --cpp-header ..\include\cef.h --capi-header ..\include\cef_capi.h --cpptoc-dir ..\libcef_dll\cpptoc --ctocpp-dir ..\libcef_dll\ctocpp
..\..\third_party\python_26\python.exe translator.py --cpp-header ..\include\cef.h --capi-header ..\include\cef_capi.h --cpptoc-global-impl ..\libcef_dll\libcef_dll.cc --ctocpp-global-impl ..\libcef_dll\wrapper\libcef_dll_wrapper.cc --cpptoc-dir ..\libcef_dll\cpptoc --ctocpp-dir ..\libcef_dll\ctocpp
pause

View File

@@ -28,10 +28,14 @@ parser.add_option('--cpp-header', dest='cppheader', metavar='FILE',
help='source CEF C++ header file [required]')
parser.add_option('--capi-header', dest='capiheader', metavar='FILE',
help='output CEF C API header file')
parser.add_option('--cpptoc-global-impl', dest='cpptocglobalimpl', metavar='FILE',
help='input/output file for CppToC global translations')
parser.add_option('--ctocpp-global-impl', dest='ctocppglobalimpl', metavar='FILE',
help='input/output file for CppToC global translations')
parser.add_option('--cpptoc-dir', dest='cpptocdir', metavar='DIR',
help='input/output directory for CppToC translations')
help='input/output directory for CppToC class translations')
parser.add_option('--ctocpp-dir', dest='ctocppdir', metavar='DIR',
help='input/output directory for CppToC translations')
help='input/output directory for CppToC class translations')
parser.add_option('--no-cpptoc-header',
action='store_true', dest='nocpptocheader', default=False,
help='do not output the CppToC headers')
@@ -91,36 +95,50 @@ else:
classes = sorted(classes)
if not options.cpptocglobalimpl is None:
# output CppToC global file
if not options.quiet:
sys.stdout.write('Generating CppToC global implementation...\n')
writect += write_cpptoc_impl(header, None, options.cpptocglobalimpl, \
not options.nobackup)
if not options.ctocppglobalimpl is None:
# output CToCpp global file
if not options.quiet:
sys.stdout.write('Generating CToCpp global implementation...\n')
writect += write_ctocpp_impl(header, None, options.ctocppglobalimpl, \
not options.nobackup)
if not options.cpptocdir is None:
#output CppToC files
#output CppToC class files
if not options.quiet:
sys.stdout.write('In CppToC directory '+options.cpptocdir+'...\n')
for cls in classes:
if not options.nocpptocheader:
if not options.quiet:
sys.stdout.write('Generating '+cls+'CppToC header...\n')
sys.stdout.write('Generating '+cls+'CppToC class header...\n')
writect += write_cpptoc_header(header, cls, options.cpptocdir,
not options.nobackup)
if not options.nocpptocimpl:
if not options.quiet:
sys.stdout.write('Generating '+cls+'CppToC implementation...\n')
sys.stdout.write('Generating '+cls+'CppToC class implementation...\n')
writect += write_cpptoc_impl(header, cls, options.cpptocdir,
not options.nobackup)
if not options.ctocppdir is None:
#output CppToC files
#output CppToC class files
if not options.quiet:
sys.stdout.write('In CToCpp directory '+options.ctocppdir+'...\n')
for cls in classes:
if not options.nocpptocheader:
if not options.quiet:
sys.stdout.write('Generating '+cls+'CToCpp header...\n')
sys.stdout.write('Generating '+cls+'CToCpp class header...\n')
writect += write_ctocpp_header(header, cls, options.ctocppdir,
not options.nobackup)
if not options.nocpptocimpl:
if not options.quiet:
sys.stdout.write('Generating '+cls+'CToCpp implementation...\n')
sys.stdout.write('Generating '+cls+'CToCpp class implementation...\n')
writect += write_ctocpp_impl(header, cls, options.ctocppdir,
not options.nobackup)

View File

@@ -1,2 +1,2 @@
#!/bin/sh
python translator.py --cpp-header ../include/cef.h --capi-header ../include/cef_capi.h --cpptoc-dir ../libcef_dll/cpptoc --ctocpp-dir ../libcef_dll/ctocpp
python translator.py --cpp-header ../include/cef.h --capi-header ../include/cef_capi.h --cpptoc-global-impl ../libcef_dll/libcef_dll.cc --ctocpp-global-impl ../libcef_dll/wrapper/libcef_dll_wrapper.cc --cpptoc-dir ../libcef_dll/cpptoc --ctocpp-dir ../libcef_dll/ctocpp