Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / mojo / nacl / generator / interface_dsl.py
blobc6b10bb821471dab0a8b4efd49795bbd5cbbaa2d
1 # Copyright 2014 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
5 class Interface(object):
6 def __init__(self):
7 self.functions = []
9 def Func(self, name, return_type):
10 f = Function(self, len(self.functions), name, return_type)
11 self.functions.append(f)
12 return f
14 def Finalize(self):
15 for f in self.functions:
16 f.Finalize()
18 class Function(object):
19 def __init__(self, parent, uid, name, return_type):
20 self.parent = parent
21 self.uid = uid
22 self.name = name
23 self.return_type = return_type
24 self.params = []
25 self.param_by_name = {}
26 self.result_param = None
28 def Param(self, name, param_type=None):
29 p = Param(self, len(self.params), name, param_type)
30 self.params.append(p)
31 self.param_by_name[name] = p
32 return p
34 def ParamList(self):
35 return [param.param_type + ' ' + param.name for param in self.params]
37 def ParamDecl(self):
38 if self.params:
39 return ', '.join(self.ParamList())
40 else:
41 return 'void'
43 def Finalize(self):
44 self.result_param = Param(self, len(self.params), 'result')
45 self.result_param.Out(self.return_type).AlwaysWritten()
47 class Param(object):
48 def __init__(self, parent, uid, name, param_type=None):
49 self.parent = parent
50 self.uid = uid
51 self.name = name
52 self.base_type = param_type
53 self.param_type = param_type
54 self.size = None
55 self.is_input = False
56 self.is_output = False
57 self.is_array = False
58 self.is_struct = False
59 self.is_extensible = False
60 self.is_optional = False
61 self.is_always_written = False
63 def GetSizeParam(self):
64 assert self.size
65 return self.parent.param_by_name[self.size]
67 def In(self, ty):
68 self.base_type = ty
69 self.param_type = ty
70 self.is_input = True
71 return self
73 def InArray(self, ty, size):
74 self.base_type = ty
75 self.param_type = 'const ' + ty + '*'
76 self.size = size
77 self.is_input = True
78 self.is_array = True
79 return self
81 # An "extensible" struct is one where we don't know the exact size - rather
82 # the first 4 bytes of the struct declare the length of the struct. This
83 # allows forwards and backwards compatibility with additive changes to the
84 # structure definition.
85 def InExtensibleStruct(self, ty):
86 self.base_type = ty
87 self.param_type = 'const struct ' + ty + '*'
88 self.is_input = True
89 self.is_struct = True
90 self.is_extensible = True
91 return self
93 def InOut(self, ty):
94 self.base_type = ty
95 self.param_type = ty + '*'
96 self.is_input = True
97 self.is_output = True
98 return self
100 def Out(self, ty):
101 self.base_type = ty
102 self.param_type = ty + '*'
103 self.is_output = True
104 return self
106 def OutArray(self, ty, size):
107 self.base_type = ty
108 self.param_type = ty + '*'
109 self.size = size
110 self.is_array = True
111 self.is_output = True
112 return self
114 # The size of the struct is fixed by the API, it cannot be extended.
115 def OutFixedStruct(self, ty):
116 self.base_type = ty
117 self.param_type = 'struct ' + ty + '*'
118 self.is_output = True
119 self.is_struct = True
120 self.is_extensible = False
121 return self
123 def OutFixedStructArray(self, ty, size):
124 self.base_type = ty
125 self.param_type = 'struct ' + ty + '*'
126 self.size = size
127 self.is_array = True
128 self.is_output = True
129 return self
131 # Declares that it is valid to pass a null pointer.
132 def Optional(self):
133 assert not self.IsPassedByValue()
134 self.is_optional = True
135 return self
137 def AlwaysWritten(self):
138 assert self.is_output, self
139 self.is_always_written = True
140 return self
142 def IsScalar(self):
143 return not self.is_array and not self.is_struct
145 def IsPassedByValue(self):
146 return not self.is_output and self.IsScalar()