コード例 #1
0
def visitTypedef(node):
    if node.constrType():
        node.aliasType().decl().accept(self)

    aliasType = types.Type(node.aliasType())
    prefix = config.state['Private Prefix']

    for d in node.declarators():
        scopedName = id.Name(d.scopedName())
        guard_name = scopedName.guard()
        fqname = scopedName.fullyQualify()

        if d.sizes():
            # Array
            marshal = output.StringStream()
            skutil.marshall(marshal, None, aliasType, d, "_a", "_s")

            unmarshal = output.StringStream()
            skutil.unmarshall(unmarshal, None, aliasType, d, "_a", "_s")

            stream.out(template.array,
                       guard_name=guard_name,
                       fqname=fqname,
                       marshal=marshal,
                       unmarshal=unmarshal,
                       private_prefix=prefix)

        elif aliasType.sequence():
            stream.out(template.sequence,
                       guard_name=guard_name,
                       fqname=fqname,
                       private_prefix=prefix)
コード例 #2
0
ファイル: main.py プロジェクト: ogata-lab/rtmsdk-mac
def visitTypedef(node):
    if node.constrType():
        node.aliasType().decl().accept(self)

    aliasType = types.Type(node.aliasType())
    prefix    = config.state['Private Prefix']

    for d in node.declarators():
        scopedName = id.Name(d.scopedName())
        guard_name = scopedName.guard()
        fqname     = scopedName.fullyQualify()

        if d.sizes():
            # Array
            marshal = output.StringStream()
            skutil.marshall(marshal, None,
                            aliasType, d, "_a", "_s")

            unmarshal = output.StringStream()
            skutil.unmarshall(unmarshal, None,
                              aliasType, d, "_a", "_s")

            stream.out(template.array,
                       guard_name     = guard_name,
                       fqname         = fqname,
                       marshal        = marshal,
                       unmarshal      = unmarshal,
                       private_prefix = prefix)

        elif aliasType.sequence():
            stream.out(template.sequence,
                       guard_name     = guard_name,
                       fqname         = fqname,
                       private_prefix = prefix)
コード例 #3
0
ファイル: main.py プロジェクト: ogata-lab/rtmsdk-mac
 def marshal(stream = stream, node = node, env = environment):
     for n in node.members():
         memberType = types.Type(n.memberType())
         for d in n.declarators():
             scopedName = id.Name(d.scopedName())
             member_name = scopedName.simple()
             skutil.marshall(stream, env, memberType, d, member_name, "_n")
     return
コード例 #4
0
ファイル: main.py プロジェクト: SD2019-2/rodrigozago
 def marshal(stream=stream, node=node, env=environment):
     for n in node.members():
         memberType = types.Type(n.memberType())
         for d in n.declarators():
             scopedName = id.Name(d.scopedName())
             member_name = scopedName.simple()
             skutil.marshall(stream, env, memberType, d, member_name, "_n")
     return
コード例 #5
0
ファイル: call.py プロジェクト: gmwu/corba_examples
 def __out_marshalArgument_shared(self, stream, is_in):
     for n, argument in enumerate(self.__arguments):
         if (is_in and not argument.is_in()) or (not is_in and not argument.is_out()):
             continue
         arg_n = "arg_" + str(n)
         argtype = types.Type(argument.paramType())
         ((h_is_const, h_is_ptr), (s_is_holder, s_is_var)) = _arg_info(argtype, argument.direction())
         if h_is_ptr:
             arg_n = "*" + arg_n
         skutil.marshall(stream, None, argtype, None, arg_n, "_n")
コード例 #6
0
ファイル: main.py プロジェクト: ogata-lab/rtmsdk-mac
 def default(stream = stream, exhaustive = exhaustive,
             hasDefault = hasDefault, defaultCase = defaultCase,
             environment = environment,
             defaultMember = defaultMember):
     if hasDefault:
         caseType = types.Type(defaultCase.caseType())
         decl = defaultCase.declarator()
         decl_scopedName = id.Name(decl.scopedName())
         decl_name = decl_scopedName.simple()
         skutil.marshall(stream, environment, caseType,
                         decl, "_pd_" + decl_name, "_n")
コード例 #7
0
 def default(stream = stream, exhaustive = exhaustive,
             hasDefault = hasDefault, defaultCase = defaultCase,
             environment = environment,
             defaultMember = defaultMember):
     if hasDefault:
         caseType = types.Type(defaultCase.caseType())
         decl = defaultCase.declarator()
         decl_scopedName = id.Name(decl.scopedName())
         decl_name = decl_scopedName.simple()
         skutil.marshall(stream, environment, caseType,
                         decl, "_pd_" + decl_name, "_n")
コード例 #8
0
ファイル: call.py プロジェクト: SD2019-2/rodrigozago
 def __out_marshalArgument_shared(self,stream,is_in):
     for n, argument in enumerate(self.__arguments):
         if (is_in and not argument.is_in()) or \
            (not is_in and not argument.is_out()): continue
         arg_n = "arg_" + str(n)
         argtype = types.Type(argument.paramType())
         ((h_is_const,h_is_ptr),(s_is_holder,s_is_var)) = \
                      _arg_info(argtype,argument.direction())
         if h_is_ptr:
             arg_n = "*" + arg_n
         skutil.marshall(stream, None, argtype, None, arg_n, "_n")
コード例 #9
0
ファイル: call.py プロジェクト: OpenModelica/OMDev
    def __out_marshalReturnedValues(self,stream):
        if not (self.__has_out_args or self.__has_return_value): return
        marshal_block = output.StringStream()

        if self.__has_return_value:
            argtype = types.Type(self.__returntype)
            ((h_is_const,h_is_ptr),(s_is_holder,s_is_var)) = \
                         _arg_info(argtype,3)
            argname = "result"
            if (h_is_ptr):
                argname = "*" + argname
            skutil.marshall(marshal_block, None, argtype, None, argname, "_n")
            
        self.__out_marshalArgument_shared(marshal_block,0)
        stream.out(template.interface_proxy_marshal_returnedvalues,
                   call_descriptor = self.__name,
                   marshal_block = marshal_block)
コード例 #10
0
ファイル: call.py プロジェクト: gmwu/corba_examples
    def __out_marshalReturnedValues(self, stream):
        if not (self.__has_out_args or self.__has_return_value): return
        marshal_block = output.StringStream()

        if self.__has_return_value:
            argtype = types.Type(self.__returntype)
            ((h_is_const,h_is_ptr),(s_is_holder,s_is_var)) = \
                         _arg_info(argtype,3)
            argname = "result"
            if (h_is_ptr):
                argname = "*" + argname
            skutil.marshall(marshal_block, None, argtype, None, argname, "_n")

        self.__out_marshalArgument_shared(marshal_block, 0)
        stream.out(template.interface_proxy_marshal_returnedvalues,
                   call_descriptor=self.__name,
                   marshal_block=marshal_block)
コード例 #11
0
ファイル: main.py プロジェクト: SD2019-2/rodrigozago
def visitException(node):
    scopedName = id.Name(node.scopedName())
    name = scopedName.simple()

    outer_environment = id.lookup(node)
    environment = outer_environment.enter(name)

    scoped_name = scopedName.fullyQualify()

    # build the default ctor, copy ctor, assignment operator
    copy_ctor_body = output.StringStream()
    default_ctor_body = output.StringStream()
    default_ctor_args = []
    assign_op_body = output.StringStream()
    has_default_ctor = 0

    for m in node.members():
        has_default_ctor = 1
        memberType = types.Type(m.memberType())
        if m.constrType():
            memberType.type().decl().accept(self)
        d_memberType = memberType.deref()

        memberType_fqname = memberType.base()

        for d in m.declarators():
            decl_scopedName = id.Name(d.scopedName())
            decl_name = decl_scopedName.simple()

            decl_dims = d.sizes()
            full_dims = decl_dims + memberType.dims()
            is_array = full_dims != []
            is_array_declarator = decl_dims != []

            memberType_name_arg = memberType.op(types.IN, environment)

            if is_array_declarator:
                # we use the internal typedef'ed type if the member is an array
                # declarator
                memberType_name_arg = "const "                       +\
                                      config.state['Private Prefix'] +\
                                      "_" + decl_name
            elif d_memberType.sequence():
                if memberType.typedef():
                    memberType_name_arg = "const " + id.Name(memberType.type(
                    ).decl().scopedName()).unambiguous(environment)
                else:
                    memberType_name_arg = "const " + memberType.sequenceTemplate(
                        environment)
            elif memberType.typecode():
                memberType_name_arg = "::CORBA::TypeCode_ptr"

            index = ""

            if is_array:
                blocks = [
                    cxx.Block(copy_ctor_body),
                    cxx.Block(default_ctor_body),
                    cxx.Block(assign_op_body)
                ]
                loops = [
                    cxx.For(copy_ctor_body, full_dims),
                    cxx.For(default_ctor_body, full_dims),
                    cxx.For(assign_op_body, full_dims)
                ]
                index = loops[0].index()  # all the same

            copy_ctor_body.out("""\
@member_name@@index@ = _s.@member_name@@index@;""",
                               member_name=decl_name,
                               index=index)

            if (d_memberType.interface() and not is_array):

                # these are special resources which need to be explicitly
                # duplicated (but not if an array?)
                duplicate = memberType_fqname.replace("_ptr", "") + \
                            "::_duplicate"

                if isinstance(d_memberType.type().decl(), idlast.Forward):
                    duplicate = duplicate.replace("::_dup", "_Helper::dup")

                default_ctor_body.out("""\
@duplicate@(_@member_name@@index@);""",
                                      duplicate=duplicate,
                                      member_name=decl_name,
                                      index=index)

            default_ctor_args.append(memberType_name_arg + " _" + decl_name)
            default_ctor_body.out("""\
@member_name@@index@ = _@member_name@@index@;""",
                                  member_name=decl_name,
                                  index=index)

            assign_op_body.out("""\
@member_name@@index@ = _s.@member_name@@index@;""",
                               member_name=decl_name,
                               index=index)

            if is_array:
                for loop in loops:
                    loop.end()
                for block in blocks:
                    block.end()

    default_ctor = output.StringStream()
    if has_default_ctor:
        default_ctor.out(template.exception_default_ctor,
                         scoped_name=scoped_name,
                         name=name,
                         ctor_args=", ".join(default_ctor_args),
                         default_ctor_body=str(default_ctor_body))

    # write the main chunk
    stream.out(template.exception,
               scoped_name=scoped_name,
               name=name,
               copy_ctor_body=str(copy_ctor_body),
               default_ctor=str(default_ctor),
               ctor_args=", ".join(default_ctor_args),
               default_ctor_body=str(default_ctor_body),
               repoID=node.repoId(),
               assign_op_body=str(assign_op_body))

    # deal with marshalling and demarshalling
    needs_marshalling = node.members() != []
    marshal = output.StringStream()
    unmarshal = output.StringStream()

    for m in node.members():
        memberType = types.Type(m.memberType())
        d_memberType = memberType.deref()
        for d in m.declarators():
            decl_scopedName = id.Name(d.scopedName())
            decl_name = decl_scopedName.simple()
            is_array_declarator = d.sizes() != []

            skutil.unmarshall(unmarshal, environment, memberType, d, decl_name,
                              "_n")

            skutil.marshall(marshal, environment, memberType, d, decl_name,
                            "_n")

    if needs_marshalling:
        stream.out(template.exception_operators,
                   scoped_name=scoped_name,
                   marshal=str(marshal),
                   unmarshal=str(unmarshal))

    return
コード例 #12
0
ファイル: main.py プロジェクト: SD2019-2/rodrigozago
def visitUnion(node):
    outer_environment = id.lookup(node)
    environment = outer_environment.enter(node.identifier())

    scopedName = id.Name(node.scopedName())
    name = scopedName.fullyQualify()

    switchType = types.Type(node.switchType())

    exhaustive = ast.exhaustiveMatch(switchType, ast.allCaseLabelValues(node))
    defaultCase = ast.defaultCase(node)
    ast.markDefaultCase(node)

    hasDefault = defaultCase != None

    # deal with types constructed here
    if node.constrType():
        node.switchType().decl().accept(self)
    for n in node.cases():
        if n.constrType():
            n.caseType().decl().accept(self)

    # --------------------------------------------------------------
    # union::operator{>>, <<}= (cdrStream& _n) [const]
    #
    # marshal/ unmarshal individual cases
    marshal_discriminator = output.StringStream()
    unmarshal_discriminator = output.StringStream()

    skutil.marshall(marshal_discriminator, environment, switchType, None,
                    "_pd__d", "_n")

    skutil.unmarshall(unmarshal_discriminator, environment, switchType, None,
                      "_pd__d", "_n")

    marshal_cases = output.StringStream()
    unmarshal_cases = output.StringStream()

    for c in node.cases():
        caseType = types.Type(c.caseType())
        decl = c.declarator()
        decl_scopedName = id.Name(decl.scopedName())
        decl_name = decl_scopedName.simple()

        if defaultCase == c:
            isDefault = 1
        else:
            isDefault = 0

        for l in c.labels():
            value = l.value()
            discrim_value = switchType.literal(value, environment)
            if l.default():
                unmarshal_cases.out("default:")
                marshal_cases.out("default:")
            else:
                unmarshal_cases.out("case " + discrim_value + ":")
                marshal_cases.out("case " + discrim_value + ":")

        marshal_cases.inc_indent()
        skutil.marshall(marshal_cases,
                        environment,
                        caseType,
                        decl,
                        "_pd_" + decl_name,
                        "_n",
                        is_union=1)
        marshal_cases.out("break;")
        marshal_cases.dec_indent()

        unmarshal_cases.inc_indent()
        unmarshal_cases.out("_pd__default = %d;" % isDefault)
        skutil.unmarshall(unmarshal_cases,
                          environment,
                          caseType,
                          decl,
                          "_pd_" + decl_name,
                          "_n",
                          is_union=1)
        unmarshal_cases.out("break;")
        unmarshal_cases.dec_indent()

    if not hasDefault and not exhaustive:
        unmarshal_cases.out("""\
default:
  _pd__default = 1;
  break;""")

    # write the operators
    stream.out(template.union_operators,
               name=name,
               marshal_discriminator=str(marshal_discriminator),
               unmarshal_discriminator=str(unmarshal_discriminator),
               marshal_cases=str(marshal_cases),
               unmarshal_cases=str(unmarshal_cases))

    return
コード例 #13
0
ファイル: main.py プロジェクト: ogata-lab/rtmsdk-mac
def visitException(node):
    scopedName = id.Name(node.scopedName())
    name = scopedName.simple()
    cxx_name = id.mapID(name)
    
    outer_environment = id.lookup(node)
    environment = outer_environment.enter(name)
    
    scoped_name = scopedName.fullyQualify()

    # build the default ctor, copy ctor, assignment operator
    copy_ctor_body = output.StringStream()
    default_ctor_body = output.StringStream()
    default_ctor_args = []
    assign_op_body = output.StringStream()
    has_default_ctor = 0

    for m in node.members():
        has_default_ctor = 1
        memberType = types.Type(m.memberType())
        if m.constrType():
            memberType.type().decl().accept(self)
        d_memberType = memberType.deref()

        memberType_fqname = memberType.base()
            
        for d in m.declarators():
            decl_scopedName = id.Name(d.scopedName())
            decl_name = decl_scopedName.simple()
            
            decl_dims = d.sizes()
            full_dims = decl_dims + memberType.dims()
            is_array = full_dims != []
            is_array_declarator = decl_dims != []

            memberType_name_arg = memberType.op(types.IN, environment)

            if is_array_declarator:
                # we use the internal typedef'ed type if the member is an array
                # declarator
                memberType_name_arg = "const "                       +\
                                      config.state['Private Prefix'] +\
                                      "_" + decl_name
            elif d_memberType.sequence():
                if memberType.typedef():
                    memberType_name_arg = "const " + id.Name(memberType.type().decl().scopedName()).unambiguous(environment)
                else:
                    memberType_name_arg = "const " + memberType.sequenceTemplate(environment)
            elif memberType.typecode():
                memberType_name_arg = "::CORBA::TypeCode_ptr"
                
            index = ""

            if is_array:
                blocks = [cxx.Block(copy_ctor_body),
                          cxx.Block(default_ctor_body),
                          cxx.Block(assign_op_body)]
                loops = [cxx.For(copy_ctor_body, full_dims),
                         cxx.For(default_ctor_body, full_dims),
                         cxx.For(assign_op_body, full_dims)]
                index = loops[0].index() # all the same

            copy_ctor_body.out("""\
@member_name@@index@ = _s.@member_name@@index@;""", member_name = decl_name,
                               index = index)

            if (d_memberType.interface() and not is_array):

                # these are special resources which need to be explicitly
                # duplicated (but not if an array?)
                duplicate = string.replace(memberType_fqname,"_ptr","") + \
                            "::_duplicate"
                if isinstance(d_memberType.type().decl(),idlast.Forward):
                    duplicate = string.replace(duplicate,"::_dup",\
                                               "_Helper::dup")
                default_ctor_body.out("""\
@duplicate@(_@member_name@@index@);""",
                                      duplicate = duplicate,
                                      member_name = decl_name,
                                      index = index)
            
            default_ctor_args.append(memberType_name_arg + " _" + decl_name)
            default_ctor_body.out("""\
@member_name@@index@ = _@member_name@@index@;""", member_name = decl_name,
                                  index = index)

            assign_op_body.out("""\
@member_name@@index@ = _s.@member_name@@index@;""", member_name = decl_name,
                               index = index)
            
            if is_array:
                for loop in loops: loop.end()
                for block in blocks: block.end()
          
        
    default_ctor = output.StringStream()
    if has_default_ctor:
        default_ctor.out(template.exception_default_ctor,
                         scoped_name = scoped_name,
                         name = name,
                         ctor_args = string.join(default_ctor_args, ", "),
                         default_ctor_body = str(default_ctor_body))

    # write the main chunk
    stream.out(template.exception,
               scoped_name = scoped_name,
               name = name,
               copy_ctor_body = str(copy_ctor_body),
               default_ctor = str(default_ctor),
               ctor_args = string.join(default_ctor_args, ", "),
               default_ctor_body = str(default_ctor_body),
               repoID = node.repoId(),
               assign_op_body = str(assign_op_body))
    

    # deal with marshalling and demarshalling
    needs_marshalling = node.members() != []
    marshal = output.StringStream()
    unmarshal = output.StringStream()
    
    for m in node.members():
        memberType = types.Type(m.memberType())
        d_memberType = memberType.deref()
        for d in m.declarators():
            decl_scopedName = id.Name(d.scopedName())
            decl_name = decl_scopedName.simple()
            is_array_declarator = d.sizes() != []
            
            skutil.unmarshall(unmarshal, environment,
                              memberType, d, decl_name, "_n")

            skutil.marshall(marshal, environment,
                            memberType, d, decl_name, "_n")

    if needs_marshalling:
        stream.out(template.exception_operators,
                   scoped_name = scoped_name,
                   marshal = str(marshal),
                   unmarshal = str(unmarshal))


    return
コード例 #14
0
ファイル: main.py プロジェクト: ogata-lab/rtmsdk-mac
def visitUnion(node):
    outer_environment = id.lookup(node)
    environment = outer_environment.enter(node.identifier())

    scopedName = id.Name(node.scopedName())
    name = scopedName.fullyQualify()

    switchType = types.Type(node.switchType())

    exhaustive = ast.exhaustiveMatch(switchType, ast.allCaseLabelValues(node))
    defaultCase = ast.defaultCase(node)
    ast.markDefaultCase(node)

    defaultMember = ""
    if defaultCase:
        defaultLabel = ast.defaultLabel(defaultCase)
        default_scopedName = id.Name(defaultCase.declarator().scopedName())
        defaultMember = default_scopedName.simple()
        
    hasDefault = defaultCase != None

    # Booleans are a special case (isn't everything?)
    booleanWrap = switchType.boolean() and exhaustive


    # deal with types constructed here
    if node.constrType():
        node.switchType().decl().accept(self)
    for n in node.cases():
        if n.constrType():
            n.caseType().decl().accept(self)

    # --------------------------------------------------------------
    # union::operator{>>, <<}= (cdrStream& _n) [const]
    #
    # marshal/ unmarshal individual cases
    marshal_discriminator = output.StringStream()
    unmarshal_discriminator = output.StringStream()
    
    skutil.marshall(marshal_discriminator,environment,
                    switchType, None, "_pd__d", "_n")
    skutil.unmarshall(unmarshal_discriminator,environment,
                      switchType, None, "_pd__d", "_n")

    marshal_cases = output.StringStream()
    unmarshal_cases = output.StringStream()
    for c in node.cases():
        caseType = types.Type(c.caseType())
        decl = c.declarator()
        decl_scopedName = id.Name(decl.scopedName())
        decl_name = decl_scopedName.simple()

        # *** HERE: only output code once for each case, no matter how
        # *** many labels; don't bother with the default check -- do
        # *** it with the switch. Don't think we need _pd__default
        # *** member.

        if defaultCase == c:
            isDefault = 1
        else:
            isDefault = 0
        
        for l in c.labels():
            value = l.value()
            discrim_value = switchType.literal(value, environment)
            if l.default():
                unmarshal_cases.out("default:")
            else:
                unmarshal_cases.out("case " + discrim_value + ":")
                marshal_cases.out("case " + discrim_value + ":")

                marshal_cases.inc_indent()
                skutil.marshall(marshal_cases, environment,
                                caseType, decl, "_pd_" + decl_name, "_n")
                marshal_cases.out("break;")
                marshal_cases.dec_indent()

            unmarshal_cases.inc_indent()
            unmarshal_cases.out("_pd__default = " + str(isDefault) + ";")
            skutil.unmarshall(unmarshal_cases, environment,
                              caseType, decl, "_pd_" + decl_name, "_n")
            unmarshal_cases.out("break;")
            unmarshal_cases.dec_indent()

    if not hasDefault and not exhaustive:
        unmarshal_cases.out("""\
default:
  _pd__default = 1;
  break;""")

            
    if booleanWrap:
        marshal_cases.out(template.union_default_bool)
    else:
        marshal_cases.out(template.union_default)


    def marshal(stream = stream, exhaustive = exhaustive,
                hasDefault = hasDefault, defaultCase = defaultCase,
                environment = environment, defaultMember = defaultMember,
                marshal_cases = marshal_cases):
        if not exhaustive:

            def default(stream = stream, exhaustive = exhaustive,
                        hasDefault = hasDefault, defaultCase = defaultCase,
                        environment = environment,
                        defaultMember = defaultMember):
                if hasDefault:
                    caseType = types.Type(defaultCase.caseType())
                    decl = defaultCase.declarator()
                    decl_scopedName = id.Name(decl.scopedName())
                    decl_name = decl_scopedName.simple()
                    skutil.marshall(stream, environment, caseType,
                                    decl, "_pd_" + decl_name, "_n")
            stream.out(template.union_operators_nonexhaustive,
                       default = default,
                       cases = str(marshal_cases))
        else:
            stream.out(template.union_operators_exhaustive,
                       cases = str(marshal_cases))

    # write the operators
    stream.out(template.union_operators,
               name = name,
               marshal_discriminator = str(marshal_discriminator),
               unmarshal_discriminator = str(unmarshal_discriminator),
               marshal_cases = marshal,
               unmarshal_cases = str(unmarshal_cases))
                
        
    return
コード例 #15
0
ファイル: main.py プロジェクト: OpenModelica/OMDev
def visitUnion(node):
    outer_environment = id.lookup(node)
    environment = outer_environment.enter(node.identifier())

    scopedName = id.Name(node.scopedName())
    name = scopedName.fullyQualify()

    switchType = types.Type(node.switchType())

    exhaustive = ast.exhaustiveMatch(switchType, ast.allCaseLabelValues(node))
    defaultCase = ast.defaultCase(node)
    ast.markDefaultCase(node)
        
    hasDefault = defaultCase != None

    # deal with types constructed here
    if node.constrType():
        node.switchType().decl().accept(self)
    for n in node.cases():
        if n.constrType():
            n.caseType().decl().accept(self)

    # --------------------------------------------------------------
    # union::operator{>>, <<}= (cdrStream& _n) [const]
    #
    # marshal/ unmarshal individual cases
    marshal_discriminator = output.StringStream()
    unmarshal_discriminator = output.StringStream()
    
    skutil.marshall(marshal_discriminator,environment,
                    switchType, None, "_pd__d", "_n")

    skutil.unmarshall(unmarshal_discriminator,environment,
                      switchType, None, "_pd__d", "_n")

    marshal_cases   = output.StringStream()
    unmarshal_cases = output.StringStream()

    for c in node.cases():
        caseType = types.Type(c.caseType())
        decl = c.declarator()
        decl_scopedName = id.Name(decl.scopedName())
        decl_name = decl_scopedName.simple()

        if defaultCase == c:
            isDefault = 1
        else:
            isDefault = 0
        
        for l in c.labels():
            value = l.value()
            discrim_value = switchType.literal(value, environment)
            if l.default():
                unmarshal_cases.out("default:")
                marshal_cases.out("default:")
            else:
                unmarshal_cases.out("case " + discrim_value + ":")
                marshal_cases.out("case " + discrim_value + ":")

        marshal_cases.inc_indent()
        skutil.marshall(marshal_cases, environment,
                        caseType, decl, "_pd_" + decl_name, "_n",
                        is_union=1)
        marshal_cases.out("break;")
        marshal_cases.dec_indent()

        unmarshal_cases.inc_indent()
        unmarshal_cases.out("_pd__default = %d;" % isDefault)
        skutil.unmarshall(unmarshal_cases, environment,
                          caseType, decl, "_pd_" + decl_name, "_n",
                          is_union=1)
        unmarshal_cases.out("break;")
        unmarshal_cases.dec_indent()


    if not hasDefault and not exhaustive:
        unmarshal_cases.out("""\
default:
  _pd__default = 1;
  break;""")

    # write the operators
    stream.out(template.union_operators,
               name = name,
               marshal_discriminator = str(marshal_discriminator),
               unmarshal_discriminator = str(unmarshal_discriminator),
               marshal_cases = str(marshal_cases),
               unmarshal_cases = str(unmarshal_cases))
                
        
    return
コード例 #16
0
def visitUnion(node):
    outer_environment = id.lookup(node)
    environment = outer_environment.enter(node.identifier())

    scopedName = id.Name(node.scopedName())
    name = scopedName.fullyQualify()

    switchType = types.Type(node.switchType())

    exhaustive = ast.exhaustiveMatch(switchType, ast.allCaseLabelValues(node))
    defaultCase = ast.defaultCase(node)
    ast.markDefaultCase(node)

    defaultMember = ""
    if defaultCase:
        defaultLabel = ast.defaultLabel(defaultCase)
        default_scopedName = id.Name(defaultCase.declarator().scopedName())
        defaultMember = default_scopedName.simple()
        
    hasDefault = defaultCase != None

    # Booleans are a special case (isn't everything?)
    booleanWrap = switchType.boolean() and exhaustive


    # deal with types constructed here
    if node.constrType():
        node.switchType().decl().accept(self)
    for n in node.cases():
        if n.constrType():
            n.caseType().decl().accept(self)

    # --------------------------------------------------------------
    # union::operator{>>, <<}= (cdrStream& _n) [const]
    #
    # marshal/ unmarshal individual cases
    marshal_discriminator = output.StringStream()
    unmarshal_discriminator = output.StringStream()
    
    skutil.marshall(marshal_discriminator,environment,
                    switchType, None, "_pd__d", "_n")
    skutil.unmarshall(unmarshal_discriminator,environment,
                      switchType, None, "_pd__d", "_n")

    marshal_cases = output.StringStream()
    unmarshal_cases = output.StringStream()
    for c in node.cases():
        caseType = types.Type(c.caseType())
        decl = c.declarator()
        decl_scopedName = id.Name(decl.scopedName())
        decl_name = decl_scopedName.simple()

        # *** HERE: only output code once for each case, no matter how
        # *** many labels; don't bother with the default check -- do
        # *** it with the switch. Don't think we need _pd__default
        # *** member.

        if defaultCase == c:
            isDefault = 1
        else:
            isDefault = 0
        
        for l in c.labels():
            value = l.value()
            discrim_value = switchType.literal(value, environment)
            if l.default():
                unmarshal_cases.out("default:")
            else:
                unmarshal_cases.out("case " + discrim_value + ":")
                marshal_cases.out("case " + discrim_value + ":")

                marshal_cases.inc_indent()
                skutil.marshall(marshal_cases, environment,
                                caseType, decl, "_pd_" + decl_name, "_n")
                marshal_cases.out("break;")
                marshal_cases.dec_indent()

            unmarshal_cases.inc_indent()
            unmarshal_cases.out("_pd__default = " + str(isDefault) + ";")
            skutil.unmarshall(unmarshal_cases, environment,
                              caseType, decl, "_pd_" + decl_name, "_n")
            unmarshal_cases.out("break;")
            unmarshal_cases.dec_indent()

    if not hasDefault and not exhaustive:
        unmarshal_cases.out("""\
default:
  _pd__default = 1;
  break;""")

            
    if booleanWrap:
        marshal_cases.out(template.union_default_bool)
    else:
        marshal_cases.out(template.union_default)


    def marshal(stream = stream, exhaustive = exhaustive,
                hasDefault = hasDefault, defaultCase = defaultCase,
                environment = environment, defaultMember = defaultMember,
                marshal_cases = marshal_cases):
        if not exhaustive:

            def default(stream = stream, exhaustive = exhaustive,
                        hasDefault = hasDefault, defaultCase = defaultCase,
                        environment = environment,
                        defaultMember = defaultMember):
                if hasDefault:
                    caseType = types.Type(defaultCase.caseType())
                    decl = defaultCase.declarator()
                    decl_scopedName = id.Name(decl.scopedName())
                    decl_name = decl_scopedName.simple()
                    skutil.marshall(stream, environment, caseType,
                                    decl, "_pd_" + decl_name, "_n")
            stream.out(template.union_operators_nonexhaustive,
                       default = default,
                       cases = str(marshal_cases))
        else:
            stream.out(template.union_operators_exhaustive,
                       cases = str(marshal_cases))

    # write the operators
    stream.out(template.union_operators,
               name = name,
               marshal_discriminator = str(marshal_discriminator),
               unmarshal_discriminator = str(unmarshal_discriminator),
               marshal_cases = marshal,
               unmarshal_cases = str(unmarshal_cases))
                
        
    return