예제 #1
0
def GetFunctionPrototype(scope, obj):
  """Gets the string needed to declare a function prototype.

  Args:
    scope: the scope of the prototype.
    obj: the function to declare.

  Returns:
    a string that is the prototype.
  """
  # For creator functions matching class name, should be capitalized
  if not obj.type_defn:
    func_name = naming.Normalize(obj.name, naming.Capitalized)
  else:
    func_name = naming.Normalize(obj.name, naming.Java)
  param_strings = []
  for p in obj.params:
    param_string = GetFunctionParamPrototype(scope, p)
    param_strings += [param_string]
  param_string = ', '.join(param_strings)

  static = ''
  if 'static' in obj.attributes:
    static = 'static'

  if obj.type_defn:
    bm = obj.type_defn.binding_model
    return_value = bm.JavaMemberString(scope, obj.type_defn)
    prototype = '%s %s %s(%s)' % (static, return_value, func_name, param_string)
  else:
    prototype = '%s %s(%s)' % (static, func_name, param_string)

  return prototype
    def Variable(self, parent_section, scope, obj):
        """Generates the code for a Variable definition.

    This function will generate the member/global variable declaration, as well
    as the setter/getter functions if specified in the attributes.

    Args:
      parent_section: the main section of the parent scope.
      scope: the parent scope.
      obj: the Variable definition.
    """
        member_section = self.GetSectionFromAttributes(parent_section, obj)

        bm = obj.type_defn.binding_model
        id_prefix = js_utils.GetFullyQualifiedScopePrefix(scope)
        proto = 'prototype.'
        # Note: There is no static in javascript
        field_name = naming.Normalize(obj.name, naming.Java)
        extra = ''
        if 'getter' in obj.attributes and 'setter' not in obj.attributes:
            extra = '\n\nThis property is read-only.'
        elif 'getter' not in obj.attributes and 'setter' in obj.attributes:
            extra = '\n\nThis property is write-only.'
        type_string = '\n@type {%s}' % js_utils.GetFullyQualifiedTypeName(
            obj.type_defn)
        self.Documentation(member_section, obj, extra + type_string)
        undef = ''
        if gflags.FLAGS['properties-equal-undefined'].value:
            undef = ' = undefined'
        member_section.EmitCode('%s%s%s%s;' %
                                (id_prefix, proto, field_name, undef))
  def Variable(self, parent_section, scope, obj):
    """Generates the code for a Variable definition.

    This function will generate the member/global variable declaration, as well
    as the setter/getter functions if specified in the attributes.

    Args:
      parent_section: the main section of the parent scope.
      scope: the parent scope.
      obj: the Variable definition.
    """
    member_section = self.GetSectionFromAttributes(parent_section, obj)

    bm = obj.type_defn.binding_model
    type_string = bm.JavaMemberString(scope, obj.type_defn)
    # Note: There is no static in javascript
    field_name = naming.Normalize(obj.name, naming.Java)
    if 'getter' in obj.attributes and 'setter' not in obj.attributes:
      self.Documentation(member_section, obj,
                         'This property is read-only.')
    elif 'getter' not in obj.attributes and 'setter' in obj.attributes:
      self.Documentation(member_section, obj,
                         'This property is write-only.')
    else:
      self.Documentation(member_section, obj, '')
    member_section.EmitCode('%s %s;' % (type_string, field_name))
    def Function(self, parent_section, scope, obj):
        """Generates the code for a Function definition.

    Args:
      parent_section: the main section of the parent scope.
      scope: the parent scope.
      obj: the Function definition.

    Returns:
      a list of (boolean, Definition) pairs, of all the types that need
      to be declared (boolean is False) or defined (boolean is True) before
      this definition.
    """
        section = self.GetSectionFromAttributes(parent_section, obj)
        self.Documentation(section, scope, obj)

        # create temporary function for generating Java syntax
        func_name = naming.Normalize(obj.name, naming.Java)
        function = syntax_tree.Function(obj.source, obj.attributes, func_name,
                                        None, [])
        function.type_defn = obj.type_defn
        function.parent = obj.parent
        function.params = obj.params
        prototype, check_types = cpp_utils.GetFunctionPrototype(
            scope, function, '')
        section.EmitCode(prototype + ';')
        return check_types
    def Variable(self, parent_section, scope, obj):
        """Generates the code for a Variable definition.

    This function will generate the member/global variable declaration, as well
    as the setter/getter functions if specified in the attributes.

    Args:
      parent_section: the main section of the parent scope.
      scope: the parent scope.
      obj: the Variable definition.

    Returns:
      a list of (boolean, Definition) pairs, of all the types that need
      to be declared (boolean is False) or defined (boolean is True) before
      this definition.
    """
        if obj.parent.defn_type == 'Class':
            if 'field_access' in obj.attributes:
                member_section = parent_section.GetSection(
                    obj.attributes['field_access'] + ':')
            else:
                member_section = parent_section.GetSection('private:')
        else:
            member_section = parent_section
        getter_section = self.GetSectionFromAttributes(parent_section, obj)

        bm = obj.type_defn.binding_model
        type_string, need_defn = bm.CppMemberString(scope, obj.type_defn)
        check_types = [(need_defn, obj.type_defn)]
        if 'static' in obj.attributes:
            static = 'static '
        else:
            static = ''
        field_name = naming.Normalize(obj.name, naming.Java)
        self.Documentation(member_section, scope, obj)
        member_section.EmitCode('%s%s %s;' % (static, type_string, field_name))
        if 'getter' in obj.attributes:
            return_type, need_defn = bm.CppReturnValueString(
                scope, obj.type_defn)
            check_types += [(need_defn, obj.type_defn)]
            getter_name = cpp_utils.GetGetterName(obj)
            self.FieldFunctionDocumentation(getter_section, 'Accessor',
                                            type_string, field_name)
            getter_section.EmitCode(
                '%s%s %s() const { return %s; }' %
                (static, return_type, getter_name, field_name))
        if 'setter' in obj.attributes:
            param_type, need_defn = bm.CppParameterString(scope, obj.type_defn)
            check_types += [(need_defn, obj.type_defn)]
            setter_name = cpp_utils.GetSetterName(obj)
            self.FieldFunctionDocumentation(getter_section, 'Mutator',
                                            type_string, field_name)
            getter_section.EmitCode('%svoid %s(%s %s) { %s = %s; }' %
                                    (static, setter_name, param_type, obj.name,
                                     field_name, obj.name))
        return check_types
예제 #6
0
  def Variable(self, context, obj):
    """Generates the code for a Variable definition.

    This function will generate the member/global variable declaration, as well
    as the setter/getter functions if specified in the attributes.

    Args:
      parent_section: the main section of the parent scope.
      obj: the Variable definition.
    """
    bm = obj.type.binding_model
    type_string, need_defn = bm.CppMemberString(context.header_scope, obj.type)
    context.CheckType(need_defn, obj.type)
    need_glue = self.NeedsGlue(obj) or (obj.parent.is_type and
                                        self.NeedsGlue(obj.parent));

    getter_attributes = {}
    if 'static' in obj.attributes:
      getter_attributes['static'] = obj.attributes['static']
      static = 'static '
    else:
      static = ''
    for attr in ['public', 'protected', 'private']:
      if attr in obj.attributes:
        getter_attributes[attr] = obj.attributes[attr]

    if not need_glue:
      if obj.parent.defn_type == 'Class':
        if 'field_access' in obj.attributes:
          member_section = context.header_section.GetSection(
              obj.attributes['field_access'] + ':')
        else:
          member_section = context.header_section.GetSection('private:')
      else:
        member_section = context.header_section
      field_name = naming.Normalize(obj.name, naming.LowerTrailing)
      member_section.EmitCode('%s%s %s;' % (static, type_string, field_name))

    if 'getter' in obj.attributes:
      func = obj.MakeGetter(getter_attributes, cpp_utils.GetGetterName(obj))
      if need_glue:
        self.FunctionGlue(context, func)
        impl = None
      else:
        impl = ' { return %s; }' % field_name
      self.FunctionDecl(context, func, impl)
    if 'setter' in obj.attributes:
      func = obj.MakeSetter(getter_attributes, cpp_utils.GetSetterName(obj))
      if need_glue:
        self.FunctionGlue(context, func)
        impl = None
      else:
        impl = ' { %s = %s; }' % (field_name, obj.name)
      self.FunctionDecl(context, func, impl)
예제 #7
0
def GetParamSpec(obj, param_name):
  """Gets the parameter specification string for a function parameter.

  Args:
    obj: The function.
    param_name: The name of the paramter.

  Returns:
    a string in JSDOC format for the parameter.
  """
  type = GetFunctionParamType(obj, param_name)
  return '@param {%s} %s ' % (type, naming.Normalize(param_name, naming.Java))
예제 #8
0
def GetSetterName(field):
    """Gets the name of the setter function for a member field.

  Unless overridden by the 'setter' attribute in IDL, the default name for the
  setter function is 'set_' concatenated with the name of the field, normalized
  to the lower-case convention.

  Args:
    field: the Definition for the field.

  Returns:
    the name of the setter function.
  """
    return (field.attributes['setter']
            or 'set_%s' % naming.Normalize(field.name, naming.Lower))
예제 #9
0
def GetFunctionPrototype(scope, obj, member):
  """Gets the string needed to declare a function prototype.

  Args:
    scope: the scope of the prototype.
    obj: the function to declare.
    member: True if member function

  Returns:
    A string prototype.
  """
  id_prefix = GetFullyQualifiedScopePrefix(scope)
  proto = ''
  if member:
    proto = 'prototype.'
  param_strings = [GetFunctionParamPrototype(scope, p)[0] for p in obj.params]
  param_string = ', '.join(param_strings)
  prototype = '%s%s%s = function(%s) { };' % (
      id_prefix, proto, naming.Normalize(obj.name, naming.Java), param_string)
  return prototype
예제 #10
0
def GetFunctionParamPrototype(scope, param):
  """Gets the string needed to declare a parameter in a function prototype.

  Args:
    scope: the scope of the prototype.
    param: the Function.Param to declare

  Returns:
    a (string, list) pair. The string is the declaration of the parameter in
    the prototype. The list contains (nam, Definition) pairs, describing the
    types that need to be forward-declared (bool is false) or defined (bool is
    true).
  """
  bm = param.type_defn.binding_model
  if param.mutable:
    text, need_defn = bm.CppMutableParameterString(scope, param.type_defn)
  else:
    text, need_defn = bm.CppParameterString(scope, param.type_defn)
  name = naming.Normalize(param.name, naming.Java)
  return name, [(name, param.type_defn)]
    def OverloadedFunction(self, parent_section, scope, func_array):
        """Generates the code for an Overloaded Function.

    Args:
      parent_section: the main section of the parent scope.
      scope: the parent scope.
      func_array: an array of function definition objects.
    """
        # merge the params.
        params = []
        min_params = len(func_array[0].params)
        for func in func_array:
            if len(func.params) < min_params:
                min_params = len(func.params)
            index = 0
            # we only take the comment from the first function that documents
            # a parameter at this position.
            comments, param_comments = js_utils.GetCommentsForParams(func)
            for param in func.params:
                if len(params) <= index:
                    params.append({
                        'orig_name': param.name,
                        'new_name': param.name,
                        'docs': param_comments[param.name],
                        'params': [{
                            'param': param,
                            'func': func
                        }]
                    })
                else:
                    params[index]['params'].append({
                        'param': param,
                        'func': func
                    })
                index += 1

        # rename the params.
        index = 0
        opt = ''
        for param in params:
            if index >= min_params:
                opt = 'opt_'
            param['new_name'] = '%sparam%d' % (opt, index + 1)
            index += 1

        # generate param comments.
        param_comments = []
        for param in params:
            if len(param['params']) == 1:
                param_string = js_utils.GetFunctionParamType(
                    param['params'][0]['func'],
                    param['params'][0]['param'].name)
            else:
                union_strings = set()
                for option in param['params']:
                    union_strings.add(
                        js_utils.GetFunctionParamType(option['func'],
                                                      option['param'].name))
                param_string = '|'.join(union_strings)
                if len(union_strings) > 1:
                    param_string = '(' + param_string + ')'
            param_comments += [
                '@param {%s} %s %s' %
                (param_string, param['new_name'], param['docs'])
            ]

        first_func = func_array[0]

        # use just the first function's comments.
        func_comments = (js_utils.GetCommentsForParams(first_func)[0] +
                         '\n'.join(param_comments))

        first_func.attributes['__docs'] = func_comments
        section = self.GetSectionFromAttributes(parent_section, first_func)
        self.Documentation(section, first_func, '')

        param_strings = []
        for param in params:
            param_strings += [param['new_name']]

        param_string = ', '.join(param_strings)
        id_prefix = js_utils.GetFullyQualifiedScopePrefix(scope)
        proto = 'prototype.'
        prototype = '%s%s%s = function(%s) { };' % (
            id_prefix, proto, naming.Normalize(first_func.name,
                                               naming.Java), param_string)
        section.EmitCode(prototype)