blob: 0cb7e2404c9757183590ad1f29e79811b9d97041 [file] [log] [blame]
////////////////////////////////////////////////////////////////////////////////
// Do not modify!
// Generated by embed
////////////////////////////////////////////////////////////////////////////////
package generate
var embedded = map[string]string{
rpc_go_tmpl_file: rpc_go_tmpl,
rpc_java_tmpl_file: rpc_java_tmpl,
rpc_common_go_tmpl_file: rpc_common_go_tmpl,
}
const rpc_go_tmpl_file = `rpc.go.tmpl`
const rpc_go_tmpl = `{{/*
* Copyright (C) 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/}}
{{Global "module" ""}}
{{Include "rpc_common_go.tmpl"}}
{{$api := Global "API"}}
{{$ | Macro "Rpc" "role" "" "api" | GoFmt | Write (print $api "_rpc.go")}}
{{$ | Macro "Rpc" "role" "Extra" "api" | GoFmt | Write (print $api "_extra.go")}}
{{$ | Macro "Rpc" "role" "Helpers" "api" | GoFmt | Write (print $api "_helpers.go")}}
{{$ | Macro "Client" | GoFmt | Write (print $api "_client.go")}}
{{$ | Macro "Server" | GoFmt | Write (print $api "_server.go")}}
{{define "Rpc"}}
{{template "GeneratedHeader"}}
{{Macro (print "Imports" $.role) $.api}}
{{range $i, $c := $.api.Functions}}
{{Macro (print "Call" $.role) $c}}
{{Macro (print "Result" $.role) $c}}
{{end}}
{{range $_, $h := $.api.Pseudonyms}}
{{if GetAnnotation $h "handle"}}
{{Macro (print "Handle" $.role) $h}}
{{end}}
{{end}}
{{range $_, $a := $.api.Slices}}
{{Macro (print "Array" $.role) $a}}
{{end}}
{{range $_, $e := $.api.Enums}}
{{Macro (print "Enum" $.role) $e}}
{{end}}
{{range $i, $c := $.api.Classes}}
{{if GetAnnotation $c "Interface"}}
{{Macro (print "Interface" $.role) $c}}¶
{{end}}
{{end}}
{{range $i, $c := $.api.Classes}}
{{if not (GetAnnotation $c "Interface")}}
{{Macro (print "Class" $.role) $c}}¶
{{end}}
{{end}}
{{end}}
{{define "Imports"}}
package {{Global "OutputDir"}};
import (
"android.googlesource.com/platform/tools/gpu/binary"
"android.googlesource.com/platform/tools/gpu/log"
)
type RPC interface {
{{range $_, $c := $.Functions}}
{{Macro "RpcInterfaceMethod" $c}}
{{end}}
}
{{end}}
{{define "ImportsExtra"}}
package {{Global "OutputDir"}};
{{end}}
{{define "ImportsHelpers"}}
package {{Global "OutputDir"}};
import (
"fmt"
"android.googlesource.com/platform/tools/gpu/binary"
)
{{end}}
{{/*
-------------------------------------------------------------------------------
Emits the interface method declaration for a single RPC command.
-------------------------------------------------------------------------------
*/}}
{{define "RpcInterfaceMethod"}}
{{AssertType $ "Function"}}
{{Macro "VerifyInterfaceReturnTypeIsRef" $}}
{{$.Name}}(l log.Logger, {{Macro "Parameters" $}})§
({{if not (IsVoid $.Return.Type)}}{{Macro "Type" $.Return.Type}}, {{end}}error)
{{end}}
{{/*
-------------------------------------------------------------------------------
Emits a comma-separated list of command parameters (name followed by type).
-------------------------------------------------------------------------------
*/}}
{{define "Parameters"}}
{{AssertType $ "Function"}}
{{range $i, $p := $.CallParameters}}
{{if $i}},{{end}}§
{{Macro "Parameter" "Parameter" $p "Command" $}}§
{{end}}
{{end}}
{{/*
-------------------------------------------------------------------------------
Emits a comma-separated list of command arguments (names).
-------------------------------------------------------------------------------
*/}}
{{define "Arguments"}}
{{AssertType $ "Function"}}
{{range $i, $p := $.CallParameters}}{{if $i}}, {{end}}§{{$p.Name}}:{{$p.Name}}{{end}}
{{end}}
{{/*
-------------------------------------------------------------------------------
Emits a single command parameter (name followed by type).
-------------------------------------------------------------------------------
*/}}
{{define "Parameter"}}
{{AssertType $.Parameter "Parameter"}}
{{AssertType $.Command "Function"}}
{{Macro "VerifyInterfaceParameterIsRef" "Parameter" $.Parameter "Command" $.Command}}
{{$.Parameter.Name}} {{Macro "Type" (TypeOf $.Parameter)}}
{{end}}
{{/*
-------------------------------------------------------------------------------
Walks a class hierarchy to visit all the fields, calling the specified macro
for each field.
-------------------------------------------------------------------------------
*/}}
{{define "AllFields"}}
{{AssertType $.Class "Class"}}
{{AssertType $.Macro "string"}}
{{Macro "TraverseFields" "Impl" $.Class "Class" $.Class "Macro" $.Macro}}
{{end}}
{{/*
-------------------------------------------------------------------------------
Walks a class hierarchy to visit all the fields, calling the specified macro
for each field.
-------------------------------------------------------------------------------
*/}}
{{define "TraverseFields"}}
{{AssertType $.Impl "Class"}}
{{AssertType $.Class "Class"}}
{{AssertType $.Macro "string"}}
{{range $_, $e := $.Class.Extends}}
{{Macro "TraverseFields" "Impl" $.Impl "Class" $e "Macro" $.Macro}}
{{end}}
{{range $_, $f := $.Class.Fields}}
{{Macro $.Macro "Impl" $.Impl "Class" $.Class "Field" $f}}
{{end}}
{{end}}
{{/*
-------------------------------------------------------------------------------
Declares a single field accessor (getter) of an interface.
-------------------------------------------------------------------------------
*/}}
{{define "DeclareInterfaceFieldAccessor"}}
{{AssertType $.Field "Field"}}
Get{{$.Field.Name}}() {{Macro "Type" (TypeOf $.Field)}}
{{end}}
{{/*
-------------------------------------------------------------------------------
Declares a single class field.
-------------------------------------------------------------------------------
*/}}
{{define "DeclareClassField"}}
{{AssertType $.Field "Field"}}
{{$.Field.Name}} {{Macro "Type" (TypeOf $.Field)}}
{{end}}
{{/*
-------------------------------------------------------------------------------
Emits a single field parameter name-type pair for the constructor of a class.
-------------------------------------------------------------------------------
*/}}
{{define "CreateClassParameter"}}
{{AssertType $.Field "Field"}}
{{$.Field.Name}} {{Macro "Type" (TypeOf $.Field)}},
{{end}}
{{/*
-------------------------------------------------------------------------------
Emits a the initializer for a single class field.
-------------------------------------------------------------------------------
*/}}
{{define "AssignClassParameter"}}
{{AssertType $.Field "Field"}}
{{$.Field.Name}}: {{$.Field.Name}},
{{end}}
{{/*
-------------------------------------------------------------------------------
Emits a single field accessor (getter) for a concrete class.
-------------------------------------------------------------------------------
*/}}
{{define "DefineClassFieldAccessor"}}
{{AssertType $.Field "Field"}}
{{AssertType $.Impl "Class"}}
func (c *{{$.Impl.Name}}) Get{{$.Field.Name}}() {{Macro "Type" (TypeOf $.Field)}} { return c.{{$.Field.Name}} }
{{end}}
{{/*
-------------------------------------------------------------------------------
Emits the interface of a class annotated with @Interface.
-------------------------------------------------------------------------------
*/}}
{{define "Interface"}}
{{AssertType $ "Class"}}
{{$iname := $.Name}}
// Interface {{$.Name}}
type {{$iname}} interface {
{{if eq (len $.Extends) 0}}
binary.Object
{{end}}
{{Macro "AllFields" "Class" $ "Macro" "DeclareInterfaceFieldAccessor"}}
}
{{end}}
{{define "InterfaceExtra"}}{{end}}
{{define "InterfaceHelpers"}}{{end}}
{{/*
-------------------------------------------------------------------------------
Emits the implementation of a class.
-------------------------------------------------------------------------------
*/}}
{{define "Class"}}
{{AssertType $ "Class"}}
{{Macro "AllFields" "Class" $ "Macro" "VerifyInterfaceFieldIsRef"}}
// Class {{$.Name}}
type {{$.Name}} struct {
binary.Generate
{{Macro "AllFields" "Class" $ "Macro" "DeclareClassField"}}
}
{{end}}
{{define "ClassExtra"}}{{end}}
{{define "ClassHelpers"}}
func Create{{$.Name}}(
{{Macro "AllFields" "Class" $ "Macro" "CreateClassParameter"}}
) *{{$.Name}} {
return &{{$.Name}} {
{{Macro "AllFields" "Class" $ "Macro" "AssignClassParameter"}}
}
}
{{Macro "AllFields" "Class" $ "Macro" "DefineClassFieldAccessor"}}
{{end}}
{{/*
-------------------------------------------------------------------------------
Emits the encoder of a handle.
-------------------------------------------------------------------------------
*/}}
{{define "Handle"}}
{{AssertType $ "Pseudonym"}}
// Handle {{$.Name}}
type {{$.Name}} struct {
binary.Generate
ID binary.ID
}
{{end}}
{{define "HandleExtra"}}{{end}}
{{define "HandleHelpers"}}
func (h {{$.Name}}) Valid() bool {
return h.ID.Valid()
}
{{end}}
{{/*
-------------------------------------------------------------------------------
Emits the definition of an array.
-------------------------------------------------------------------------------
*/}}
{{define "Array"}}
{{AssertType $ "Slice"}}
// Array {{$.Name}}
type {{Macro "Type" $}} []{{Macro "Type" $.To}}
{{end}}
{{define "ArrayExtra"}}
{{AssertType $ "Slice"}}
{{end}}
{{define "ArrayHelpers"}}
{{AssertType $ "Slice"}}
func (a {{Macro "Type" $}}) Format(f fmt.State, c rune) {
fmt.Fprintf(f, "[%d]{{$.Name}}", len(a))
}
{{end}}
{{/*
-------------------------------------------------------------------------------
Emits the definition of an enum.
-------------------------------------------------------------------------------
*/}}
{{define "Enum"}}
{{AssertType $ "Enum"}}
// Enum {{$.Name}}
type {{$.Name}} int
const (
{{range $_, $e := $.Entries}}
{{$.Name}}{{$e.Name}} {{$.Name}}= {{$e.Value}}
{{end}}
)
{{end}}
{{define "EnumExtra"}}
{{AssertType $ "Enum"}}
{{end}}
{{define "EnumHelpers"}}
{{AssertType $ "Enum"}}
{{range $_, $e := $.Entries}}
func (i {{$.Name}}) Is{{$e.Name}}() bool { return i == {{$.Name}}{{$e.Name}} }
{{end}}
{{end}}
{{/*
-------------------------------------------------------------------------------
Emits the name of the call type for a single command.
-------------------------------------------------------------------------------
*/}}
{{define "CallName"}}
{{AssertType $ "Function"}}
{{print "call" $.Name}}
{{end}}
{{/*
-------------------------------------------------------------------------------
Emits the name of the result type for a single command.
-------------------------------------------------------------------------------
*/}}
{{define "ResultName"}}
{{AssertType $ "Function"}}
{{print "result" $.Name}}
{{end}}
{{/*
-------------------------------------------------------------------------------
Emits the call structure for a single command.
-------------------------------------------------------------------------------
*/}}
{{define "Call"}}
{{AssertType $ "Function"}}
{{end}}
{{define "CallExtra"}}
{{AssertType $ "Function"}}
// Call {{$.Name}}
type {{Macro "CallName" $}} struct {
binary.Generate
{{range $i, $p := $.CallParameters}}
{{$p.Name}} {{Macro "Type" (TypeOf $p)}}
{{end}}
}
{{end}}
{{define "CallHelpers"}}
{{AssertType $ "Function"}}
func (c {{Macro "CallName" $}}) Format(f fmt.State, r rune) {
fmt.Fprintf(f, "{{$.Name}}(§
{{range $i, $p := $.CallParameters}}{{if $i}}, {{end}}{{$p.Name}}: %v{{end}})",
{{range $i, $p := $.CallParameters}}c.{{$p.Name}},{{end}}
)
}
{{end}}
{{/*
-------------------------------------------------------------------------------
Emits the result structure for a single command.
-------------------------------------------------------------------------------
*/}}
{{define "Result"}}
{{AssertType $ "Function"}}
{{end}}
{{define "ResultExtra"}}
{{AssertType $ "Function"}}
// Result {{$.Name}}
type {{Macro "ResultName" $}} struct {
binary.Generate
{{if not (IsVoid $.Return.Type)}}
value {{Macro "Type" $.Return.Type}}
{{end}}
}
{{end}}
{{define "ResultHelpers"}}
{{AssertType $ "Function"}}
func (r {{Macro "ResultName" $}}) Format(f fmt.State, c rune) {
{{if not (IsVoid $.Return.Type)}}
fmt.Fprintf(f, "res: %#v", r.value)
{{else}}
fmt.Fprintf(f, "void")
{{end}}
}
{{end}}
{{/*
-------------------------------------------------------------------------------
Emits the RPC client implementation
-------------------------------------------------------------------------------
*/}}
{{define "Client"}}
{{AssertType $ "API"}}
{{template "GeneratedHeader"}}
package {{Global "OutputDir"}};
import (
"io"
"android.googlesource.com/platform/tools/gpu/log"
"android.googlesource.com/platform/tools/gpu/rpc"
)
type client struct {
rpc.Client
}
func CreateClient(r io.Reader, w io.Writer, mtu int) RPC {
return client{rpc.NewClient(r, w, mtu)}
}
// Client compliance
{{range $_, $c := $.Functions}}
{{if not (IsVoid $c.Return.Type)}}
func (c client) {{$c.Name}}(l log.Logger, {{Macro "Parameters" $c}}) (res {{Macro "Type" $c.Return.Type}}, err error) {
var val interface{}
if val, err = c.Send(&{{Macro "CallName" $c}}{ {{Macro "Arguments" $c}} }); err == nil {
res = val.(*{{Macro "ResultName" $c}}).value
} else {
l.Errorf("RPC {{$c.Name}} failed with error: %v", err)
}
return
}
{{else}}
func (c client) {{$c.Name}}(l log.Logger, {{Macro "Parameters" $c}}) error {
_, err := c.Send(&{{Macro "CallName" $c}}{ {{Macro "Arguments" $c}} })
return err
}
{{end}}
{{end}}
{{end}}
{{/*
-------------------------------------------------------------------------------
Emits the RPC server implementation
-------------------------------------------------------------------------------
*/}}
{{define "Server"}}
{{AssertType $ "API"}}
{{template "GeneratedHeader"}}
package {{Global "OutputDir"}};
import (
"fmt"
"io"
"runtime/debug"
"android.googlesource.com/platform/tools/gpu/log"
"android.googlesource.com/platform/tools/gpu/rpc"
)
func BindServer(r io.Reader, w io.Writer, mtu int, l log.Logger, server RPC) {
rpc.Serve(l, r, w, mtu, func(in interface{}) (res binary.Object) {
l := l.Fork().Enter(fmt.Sprintf("%v", in))
defer func() {
if err := recover(); err == nil {
l.Infof("↪ %v", res)
} else {
msg := fmt.Sprintf("Panic: %v\n%v", err, string(debug.Stack()))
l.Errorf(msg)
res = rpc.NewError(msg)
}
}()
switch call := in.(type) {
{{range $i, $c := $.Functions}}
case *{{Macro "CallName" $c}}:
{{if not (IsVoid $c.Return.Type)}}
if res, err := server.{{$c.Name}}(l{{range $p := $c.CallParameters}}, call.{{$p.Name}}{{end}}); err == nil {
return &{{Macro "ResultName" $c}}{ value:res }
} else {
return rpc.NewError(err.Error())
}
{{else}}
if err := server.{{$c.Name}}(l{{range $p := $c.CallParameters}}, call.{{$p.Name}}{{end}}); err == nil {
return &{{Macro "ResultName" $c}}{}
} else {
return rpc.NewError(err.Error())
}
{{end}}
{{end}}
default:
return rpc.NewError("Unexpected RPC function: %T", call)
}
})
}
{{end}}
`
const rpc_java_tmpl_file = `rpc.java.tmpl`
const rpc_java_tmpl = `{{/*
* Copyright (C) 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/}}
{{Global "module" ""}}
{{Global "indent" " "}}
{{Global "member" "my"}}
{{$ | Macro "ClientInterface" | Reflow 2 | Write "Client.java"}}
{{$ | Macro "ClientImplementation" | Reflow 2 | Write "ClientImpl.java"}}
{{$ | Macro "Commands" | Reflow 2 | Write "Commands.java"}}
{{range $_, $h := $.Pseudonyms}}
{{if GetAnnotation $h "handle"}}
{{$h | Macro "Handle" | Reflow 2 | Write (print $h.Name ".java")}}
{{end}}
{{end}}
{{range $_, $e := $.Enums}}
{{$e | Macro "Enum" | Reflow 2 | Write (print $e.Name ".java")}}
{{end}}
{{range $i, $c := $.Classes }}
{{if GetAnnotation $c "Interface"}}
{{$c | Macro "Interface" | Reflow 2 | Write (print $c.Name ".java")}}
{{else}}
{{$c | Macro "Class" | Reflow 2 | Write (print $c.Name ".java")}}
{{end}}
{{end}}
{{/*
-------------------------------------------------------------------------------
Emits the AOSP copyright notice.
-------------------------------------------------------------------------------
*/}}
{{define "CopyrightHeader"}}{{Copyright "generated_aosp_java" "rpcapi"}}{{end}}
{{/*
-------------------------------------------------------------------------------
Emits the fully qualified name (prefixed with the package) of the specified
type or variable.
-------------------------------------------------------------------------------
*/}}
{{define "QualifiedName"}}{{Global "module"}}{{$.Name}}{{end}}
{{/*
-------------------------------------------------------------------------------
Emits the java type for the provided type.
-------------------------------------------------------------------------------
*/}}
{{define "Type"}}
{{AssertType $ "Type"}}
{{if GetAnnotation $ "handle"}}{{Macro "QualifiedName" $}}
{{else if IsSlice $}}{{Macro "Type" $.ValueType}}[]
{{else if IsMap $}}{{Macro "QualifiedName" $}}
{{else if IsClass $}}{{Macro "QualifiedName" $}}
{{else if IsPointer $}}{{Macro "QualifiedName" $.To}}
{{else if IsEnum $}}{{Macro "QualifiedName" $}}
{{else if IsBool $}}boolean
{{else if IsS8 $}}byte
{{else if IsU8 $}}byte
{{else if IsS16 $}}short
{{else if IsU16 $}}short
{{else if IsS32 $}}int
{{else if IsU32 $}}int
{{else if IsF32 $}}float
{{else if IsS64 $}}long
{{else if IsU64 $}}long
{{else if IsF64 $}}double
{{else if IsString $}}String
{{else}}{{Error "macro Type called with unsupported type: %v" $.Name}}
{{end}}
{{end}}
{{/*
-------------------------------------------------------------------------------
Walks a class hierarchy to visit all the fields, calling the specified macro
for each field.
-------------------------------------------------------------------------------
*/}}
{{define "AllFields"}}
{{AssertType $.Class "Class"}}
{{AssertType $.Macro "string"}}
{{Macro "TraverseFields" "Derived" false "Class" $.Class "Macro" $.Macro}}
{{end}}
{{/*
-------------------------------------------------------------------------------
Walks a class hierarchy to visit all the fields, calling the specified macro
for each field.
-------------------------------------------------------------------------------
*/}}
{{define "TraverseFields"}}
{{AssertType $.Class "Class"}} {{/* the class holding the fields to traverse */}}
{{AssertType $.Macro "string"}} {{/* the macro to invoke */}}
{{range $_, $e := $.Class.Extends}}
{{Macro "TraverseFields" "Derived" true "Class" $e "Macro" $.Macro}}
{{end}}
{{range $_, $f := $.Class.Fields}}
{{Macro $.Macro "Derived" $.Derived "Class" $.Class "Field" $f}}
{{end}}
{{end}}
{{/*
-------------------------------------------------------------------------------
Emits a comma-separated list of java parameter names for the given RPC cmd.
-------------------------------------------------------------------------------
*/}}
{{define "Arguments"}}
{{AssertType $ "Function"}}
{{range $i, $p := $.CallParameters}}
{{if $i}}, {{end}}{{$p.Name}}§
{{end}}
{{end}}
{{/*
-------------------------------------------------------------------------------
Emits a comma-separated list of java type-name paired parameters for the given
RPC cmd.
-------------------------------------------------------------------------------
*/}}
{{define "Parameters"}}
{{AssertType $ "Function"}}
{{range $i, $p := $.CallParameters}}
{{if $i}}, {{end}}{{Macro "Parameter" "Type" (TypeOf $p) "Name" $p.Name}}§
{{end}}
{{end}}
{{/*
-------------------------------------------------------------------------------
Emits a single java type-name pair for a given RPC parameter.
-------------------------------------------------------------------------------
*/}}
{{define "Parameter"}}
{{AssertType $.Type "Type"}}
{{AssertType $.Name "string"}}
{{Macro "Type" $.Type}} {{$.Name}}
{{end}}
{{/*
-------------------------------------------------------------------------------
Emits the client RPC interface.
-------------------------------------------------------------------------------
*/}}
{{define "ClientInterface"}}
{{AssertType $ "API"}}
{{template "CopyrightHeader"}}
package {{Global "package"}};
import com.android.tools.rpclib.rpccore.RpcException;
import java.io.IOException;
import java.util.concurrent.Future;
public interface Client {
{{range $_, $c := $.Functions}}
Future<{{Macro "Type" $c.Return.Type}}> {{$c.Name}}({{Macro "Parameters" $c}}) throws IOException, RpcException;
{{end}}
}
{{end}}
{{/*
-------------------------------------------------------------------------------
Emits the client RPC interface implementation.
-------------------------------------------------------------------------------
*/}}
{{define "ClientImplementation"}}
{{AssertType $ "API"}}
{{template "CopyrightHeader"}}
package {{Global "package"}};
import com.android.tools.rpclib.rpccore.Broadcaster;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
public class ClientImpl implements Client {
private final Broadcaster myBroadcaster;
private final ExecutorService myExecutorService;
public ClientImpl(ExecutorService executorService, InputStream in, OutputStream out, int mtu) {
myExecutorService = executorService;
myBroadcaster = new Broadcaster(in, out, mtu, myExecutorService);
}
{{range $_, $c := $.Functions}}
@Override
public Future<{{Macro "Type" $c.Return.Type}}> {{$c.Name}}({{Macro "Parameters" $c}}) {
return myExecutorService.submit(new {{$c.Name}}Callable({{Macro "Arguments" $c}}));
{{end}}
{{range $i, $c := $.Functions}}
private class {{$c.Name}}Callable implements Callable<{{Macro "Type" $c.Return.Type}}> {
private final Commands.{{$c.Name}}.Call myCall;
private {{$c.Name}}Callable({{Macro "Parameters" $c}}) {
myCall = new Commands.{{$c.Name}}.Call({{Macro "Arguments" $c}});
}
@Override
public {{Macro "Type" $c.Return.Type}} call() throws Exception {
Commands.{{$c.Name}}.Result result = (Commands.{{$c.Name}}.Result)myBroadcaster.Send(myCall);
return result.myValue;
}
}
{{end}}
}
{{end}}
{{/*
-------------------------------------------------------------------------------
Emits a java class containing all the command calls and results.
-------------------------------------------------------------------------------
*/}}
{{define "Commands"}}
{{AssertType $ "API"}}
{{template "CopyrightHeader"}}
package {{Global "package"}};
import com.android.tools.rpclib.binary.Decoder;
import com.android.tools.rpclib.binary.Encoder;
import com.android.tools.rpclib.binary.ObjectTypeID;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
final class Commands {
{{range $i, $c := $.Functions}}
static final class {{$c.Name}} {
{{Macro "Call" $c}}¶
{{Macro "Result" $c}}¶
}
{{end}}
}
{{end}}
{{/*
-------------------------------------------------------------------------------
Emits the java call class for a single command.
-------------------------------------------------------------------------------
*/}}
{{define "Call"}}
{{AssertType $ "Function"}}
static final class Call implements com.android.tools.rpclib.rpccore.Call {
{{range $i, $p := $.CallParameters}}
{{Macro "Type" (TypeOf $p)}} {{Macro "ParameterAsFieldName" $p}};
{{end}}
{{if len ($.CallParameters)}}¶{{end}}
Call() {
}
{{if len $.CallParameters}}¶
Call({{Macro "Parameters" $}}) {
{{range $i, $p := $.CallParameters}}
{{Macro "ParameterAsFieldName" $p}} = {{$p.Name}};
{{end}}
}
{{end}}
@Override
public ObjectTypeID type() {
return ObjectFactory.call{{$.Name}}ID;
}
@Override
public void encode(@NotNull Encoder e) throws IOException {
ObjectFactory.encode(e, this);
}
@Override
public void decode(@NotNull Decoder d) throws IOException {
ObjectFactory.decode(d, this);
}
}
{{end}}
{{/*
-------------------------------------------------------------------------------
Emits the java result structure for a single command.
-------------------------------------------------------------------------------
*/}}
{{define "Result"}}
{{AssertType $ "Function"}}
static final class Result implements com.android.tools.rpclib.rpccore.Result {
{{if not (IsVoid $.Return.Type)}}
{{Macro "Type" $.Return.Type}} myValue;
{{end}}
@Override
public ObjectTypeID type() {
return ObjectFactory.result{{$.Name}}ID;
}
@Override
public void encode(@NotNull Encoder e) throws IOException {
ObjectFactory.encode(e, this);
}
@Override
public void decode(@NotNull Decoder d) throws IOException {
ObjectFactory.decode(d, this);
}
}
{{end}}
{{/*
-------------------------------------------------------------------------------
Emits the java implements statement for an RPC class.
-------------------------------------------------------------------------------
*/}}
{{define "ClassImplements"}}
{{AssertType $ "Class"}}
implements §
{{if len $.Extends}}{{(index $.Extends 0).Name}}§
{{else}} BinaryObject§
{{end}}
{{end}}
{{/*
-------------------------------------------------------------------------------
Emits the java interface for an RPC class annotated with @Interface.
-------------------------------------------------------------------------------
*/}}
{{define "Interface"}}
{{AssertType $ "Class"}}
{{template "CopyrightHeader"}}
package {{Global "package"}};
import com.android.tools.rpclib.binary.BinaryObject;
public interface {{$.Name}} extends BinaryObject {
{{range $_, $f := $.Fields}}
{{Macro "Type" (TypeOf $f)}} get{{$f.Name}}();¶
void set{{$f.Name}}({{Macro "Type" (TypeOf $f)}} v);¶
{{end}}§
}
{{end}}
{{/*
-------------------------------------------------------------------------------
Emits the Java name for the given field
-------------------------------------------------------------------------------
*/}}
{{define "FieldName"}}{{AssertType $ "Field"}}my{{Title $.Name}}{{end}}
{{/*
-------------------------------------------------------------------------------
Emits the Java field name inferred from the given field
-------------------------------------------------------------------------------
*/}}
{{define "ParameterAsFieldName"}}{{AssertType $ "Parameter"}}my{{Title $.Name}}
{{end}}
{{/*
-------------------------------------------------------------------------------
Declares a single class field
-------------------------------------------------------------------------------
*/}}
{{define "DeclareField"}}
{{AssertType $.Field "Field"}}
{{Macro "Type" (TypeOf $.Field)}} {{Macro "FieldName" $.Field}};
{{end}}
{{/*
-------------------------------------------------------------------------------
Emits a getter/setter pair implementation of a single field
-------------------------------------------------------------------------------
*/}}
{{define "FieldGetterSetter"}}
{{AssertType $.Field "Field"}}
{{Macro "FieldGetter" $}}¶
{{Macro "FieldSetter" $}}¶
{{end}}
{{/*
-------------------------------------------------------------------------------
Emits the getter implementation of a single field
-------------------------------------------------------------------------------
*/}}
{{define "FieldGetter"}}
{{AssertType $.Field "Field"}}
{{if $.Derived}}@Override{{end}}
public {{Macro "Type" (TypeOf $.Field)}} get{{$.Field.Name}}() {
return {{Macro "FieldName" $.Field}};
}
{{end}}
{{/*
-------------------------------------------------------------------------------
Emits the setter implementation of a single field
-------------------------------------------------------------------------------
*/}}
{{define "FieldSetter"}}
{{AssertType $.Field "Field"}}
{{if $.Derived}}@Override{{end}}
public void set{{$.Field.Name}}({{Macro "Type" (TypeOf $.Field)}} v) {
{{Macro "FieldName" $.Field}} = v;
}
{{end}}
{{/*
-------------------------------------------------------------------------------
Emits the java class for an RPC class.
-------------------------------------------------------------------------------
*/}}
{{define "Class"}}
{{AssertType $ "Class"}}
{{template "CopyrightHeader"}}
package {{Global "package"}};
{{if not (len $.Extends)}}import com.android.tools.rpclib.binary.BinaryObject;
{{end}}
import com.android.tools.rpclib.binary.Decoder;
import com.android.tools.rpclib.binary.Encoder;
import com.android.tools.rpclib.binary.ObjectTypeID;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
public class {{$.Name}} {{Macro "ClassImplements" $}} {
{{Macro "AllFields" "Class" $ "Macro" "DeclareField"}}
// Constructs a default-initialized {@link {{$.Name}}}.
public {{$.Name}}() {
}
// Constructs and decodes a {@link {{$.Name}}} from the {@link Decoder} d.
public {{$.Name}}(Decoder d) throws IOException {
decode(d);
}
{{Macro "AllFields" "Class" $ "Macro" "FieldGetterSetter"}}
@Override
public void encode(@NotNull Encoder e) throws IOException {
ObjectFactory.encode(e, this);
}
@Override
public void decode(@NotNull Decoder d) throws IOException {
ObjectFactory.decode(d, this);
}
@Override
public ObjectTypeID type() {
return ObjectFactory.{{$.Name}}ID;
}
}
{{end}}
{{/*
-------------------------------------------------------------------------------
Emits the java class for an RPC enum.
-------------------------------------------------------------------------------
*/}}
{{define "Enum"}}
{{AssertType $ "Enum"}}
{{template "CopyrightHeader"}}
package {{Global "package"}};
import com.android.tools.rpclib.binary.Decoder;
import com.android.tools.rpclib.binary.Encoder;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
public enum {{$.Name}} {
{{range $i, $e := $.Entries}}
{{if $i}}§,{{end}}
{{$e.Name}}({{$e.Value}})
{{end}}§;
final int myValue;
{{$.Name}}(int value) {
myValue = value;
}
public static {{$.Name}} decode(@NotNull Decoder d) throws IOException {
int id = d.int32();
switch (id) {
{{range $i, $e := $.Entries}}
case {{$e.Value}}:»
return {{$e.Name}};«
{{end}}
default:»
throw new RuntimeException("Unknown {{$.Name}} " + id);«
}
}
public void encode(@NotNull Encoder e) throws IOException {
e.int32(myValue);
}
}
{{end}}
{{/*
-------------------------------------------------------------------------------
Emits the definition of a single resource identifier.
-------------------------------------------------------------------------------
*/}}
{{define "Handle"}}
{{AssertType $ "Pseudonym"}}
{{template "CopyrightHeader"}}
package {{Global "package"}};
import com.android.tools.rpclib.binary.Decoder;
import com.android.tools.rpclib.binary.Handle;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
public class {{$.Name}} extends Handle {
public {{$.Name}}(@NotNull byte[] value) {
super(value);
}
public {{$.Name}}(@NotNull Decoder d) throws IOException {
super(d);
}
}
{{end}}
`
const rpc_common_go_tmpl_file = `rpc_common_go.tmpl`
const rpc_common_go_tmpl = `{{/*
* Copyright (C) 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/}}
{{/*
-------------------------------------------------------------------------------
Emits the fully qualified name (prefixed with the package) of the specified
type or variable.
Single argument:
The name to fully qualify
-------------------------------------------------------------------------------
*/}}
{{define "Go.RPC.QualifiedName"}}
{{AssertType $ "string"}}
{{Global "module"}}{{$}}
{{end}}
{{/*
-------------------------------------------------------------------------------
Verifies that the field is not a direct reference to an interface type.
-------------------------------------------------------------------------------
*/}}
{{define "VerifyInterfaceFieldIsRef"}}
{{AssertType $.Field "Field"}}
{{AssertType $.Class "Class"}}
{{$c := TypeOf $.Field}}
{{if IsClass $c}}
{{if (GetAnnotation $c "Interface")}}
{{Error $.Name "." $.Field.Name " is a non-reference to interface type " $c.Name ". This must be stored as ref<" $c.Name ">."}}
{{end}}
{{end}}
{{end}}
{{/*
-------------------------------------------------------------------------------
Verifies that the parameter is not a direct reference to an interface type.
-------------------------------------------------------------------------------
*/}}
{{define "VerifyInterfaceParameterIsRef"}}
{{AssertType $.Parameter "Parameter"}}
{{AssertType $.Command "Function"}}
{{$c := TypeOf $.Parameter}}
{{if IsClass $c}}
{{if (GetAnnotation $c "Interface")}}
{{Error $.Command.Name " parameter " $.Parameter.Name " is a non-reference to interface type " $c.Name ". This must be passed as ref<" $c.Name ">."}}
{{end}}
{{end}}
{{end}}
{{/*
-------------------------------------------------------------------------------
Verifies that the return type of a command is not a direct reference to an
interface type.
-------------------------------------------------------------------------------
*/}}
{{define "VerifyInterfaceReturnTypeIsRef"}}
{{AssertType $ "Function"}}
{{if IsClass $.Return.Type}}
{{if (GetAnnotation $.Return.Type "Interface")}}
{{Error $.Name " return value is a non-reference to interface type " $.Return.Type.Name ". This must be passed as ref<" $.Return.Type.Name ">."}}
{{end}}
{{end}}
{{end}}
{{/*
-------------------------------------------------------------------------------
Emits the go type for the provided AST type.
-------------------------------------------------------------------------------
*/}}
{{define "Type"}}
{{AssertType $ "Type"}}
{{ if IsSlice $}}{{Macro "ArrayType" $.To}}
{{else if GetAnnotation $ "handle"}}{{Macro "Go.RPC.QualifiedName" $.Name}}
{{else if IsAny $}}interface{}
{{else if IsMap $}}{{Macro "Go.RPC.QualifiedName" $.Name}}
{{else if IsClass $}}{{Macro "Go.RPC.QualifiedName" $.Name}}
{{else if IsPointer $}}{{if not (GetAnnotation $.To "Interface")}}*{{end}}{{Macro "Go.RPC.QualifiedName" $.To.Typename}}
{{else if IsEnum $}}{{Macro "Go.RPC.QualifiedName" $.Name}}
{{else if IsBool $}}bool
{{else if IsS8 $}}int8
{{else if IsU8 $}}uint8
{{else if IsS16 $}}int16
{{else if IsU16 $}}uint16
{{else if IsS32 $}}int32
{{else if IsU32 $}}uint32
{{else if IsF32 $}}float32
{{else if IsS64 $}}int64
{{else if IsU64 $}}uint64
{{else if IsF64 $}}float64
{{else if IsString $}}string
{{else}}{{Error "macro Type called with unsupported type: %v (%T)" $.Name $.Detail}}
{{end}}
{{end}}
{{/*
-------------------------------------------------------------------------------
Emits the go type for the provided AST type.
-------------------------------------------------------------------------------
*/}}
{{define "ArrayType"}}
{{ if IsBool $}}{{Macro "Go.RPC.QualifiedName" "BoolArray"}}
{{else if IsS8 $}}{{Macro "Go.RPC.QualifiedName" "S8Array"}}
{{else if IsU8 $}}{{Macro "Go.RPC.QualifiedName" "U8Array"}}
{{else if IsS16 $}}{{Macro "Go.RPC.QualifiedName" "S16Array"}}
{{else if IsU16 $}}{{Macro "Go.RPC.QualifiedName" "U16Array"}}
{{else if IsS32 $}}{{Macro "Go.RPC.QualifiedName" "I32Array"}}
{{else if IsU32 $}}{{Macro "Go.RPC.QualifiedName" "U32Array"}}
{{else if IsF32 $}}{{Macro "Go.RPC.QualifiedName" "F32Array"}}
{{else if IsS64 $}}{{Macro "Go.RPC.QualifiedName" "S64Array"}}
{{else if IsU64 $}}{{Macro "Go.RPC.QualifiedName" "U64Array"}}
{{else if IsF64 $}}{{Macro "Go.RPC.QualifiedName" "F64Array"}}
{{else if IsString $}}{{Macro "Go.RPC.QualifiedName" "StringArray"}}
{{else if IsPointer $}}{{Macro "Type" $.To}}PtrArray
{{else }}{{Macro "Type" $}}Array
{{end}}
{{end}}
{{/*
-------------------------------------------------------------------------------
Emits a comment stating that the file is automatically generated.
-------------------------------------------------------------------------------
*/}}
{{define "GeneratedHeader"}}{{Copyright "generated_by" "rpcapi"}}¶{{end}}
{{/*
-------------------------------------------------------------------------------
Emits a comment block containing the specified text.
-------------------------------------------------------------------------------
*/}}
{{define "CommentHeader"}}
////////////////////////////////////////////////////////////////////////////////
// {{.}}
////////////////////////////////////////////////////////////////////////////////
{{end}}
`