OLD | NEW |
| (Empty) |
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. | |
4 | |
5 class Interface(object): | |
6 def __init__(self): | |
7 self.functions = [] | |
8 | |
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 | |
13 | |
14 def Finalize(self): | |
15 for f in self.functions: | |
16 f.Finalize() | |
17 | |
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 | |
27 self.broken_in_nacl = False | |
28 | |
29 def Param(self, name, param_type=None): | |
30 p = Param(self, len(self.params), name, param_type) | |
31 self.params.append(p) | |
32 self.param_by_name[name] = p | |
33 return p | |
34 | |
35 def ParamList(self): | |
36 return [param.param_type + ' ' + param.name for param in self.params] | |
37 | |
38 def ParamDecl(self): | |
39 if self.params: | |
40 return ', '.join(self.ParamList()) | |
41 else: | |
42 return 'void' | |
43 | |
44 def IsBrokenInNaCl(self): | |
45 self.broken_in_nacl = True | |
46 | |
47 def Finalize(self): | |
48 self.result_param = Param(self, len(self.params), 'result') | |
49 self.result_param.Out(self.return_type).AlwaysWritten() | |
50 | |
51 class Param(object): | |
52 def __init__(self, parent, uid, name, param_type=None): | |
53 self.parent = parent | |
54 self.uid = uid | |
55 self.name = name | |
56 self.base_type = param_type | |
57 self.param_type = param_type | |
58 self.size = None | |
59 self.is_input = False | |
60 self.is_output = False | |
61 self.is_array = False | |
62 self.is_struct = False | |
63 self.is_extensible = False | |
64 self.is_optional = False | |
65 self.is_always_written = False | |
66 self.is_pointer = False | |
67 | |
68 def GetSizeParam(self): | |
69 assert self.size | |
70 return self.parent.param_by_name[self.size] | |
71 | |
72 def In(self, ty): | |
73 self.base_type = ty | |
74 self.param_type = ty | |
75 self.is_input = True | |
76 self.is_pointer = ty.endswith('*') | |
77 return self | |
78 | |
79 def InArray(self, ty, size): | |
80 self.base_type = ty | |
81 self.param_type = 'const ' + ty + '*' | |
82 self.size = size | |
83 self.is_input = True | |
84 self.is_array = True | |
85 return self | |
86 | |
87 # An "extensible" struct is one where we don't know the exact size - rather | |
88 # the first 4 bytes of the struct declare the length of the struct. This | |
89 # allows forwards and backwards compatibility with additive changes to the | |
90 # structure definition. | |
91 def InExtensibleStruct(self, ty): | |
92 self.base_type = ty | |
93 self.param_type = 'const struct ' + ty + '*' | |
94 self.is_input = True | |
95 self.is_struct = True | |
96 self.is_extensible = True | |
97 return self | |
98 | |
99 def InOut(self, ty): | |
100 self.base_type = ty | |
101 self.param_type = ty + '*' | |
102 self.is_input = True | |
103 self.is_output = True | |
104 self.is_pointer = ty.endswith('*') | |
105 return self | |
106 | |
107 def Out(self, ty): | |
108 self.base_type = ty | |
109 self.param_type = ty + '*' | |
110 self.is_output = True | |
111 self.is_pointer = ty.endswith('*') | |
112 return self | |
113 | |
114 def OutArray(self, ty, size): | |
115 self.base_type = ty | |
116 self.param_type = ty + '*' | |
117 self.size = size | |
118 self.is_array = True | |
119 self.is_output = True | |
120 return self | |
121 | |
122 # Out extensible structs have an input size indicating the buffer size. On | |
123 # success, the size actually written is indicated in the struct. | |
124 def OutExtensibleStruct(self, ty, size): | |
125 self.base_type = ty | |
126 self.param_type = 'struct ' + ty + '*' | |
127 self.size = size | |
128 self.is_array = False | |
129 self.is_output = True | |
130 self.is_struct = True | |
131 self.is_extensible = True | |
132 return self | |
133 | |
134 # The size of the struct is fixed by the API, it cannot be extended. | |
135 def OutFixedStruct(self, ty): | |
136 self.base_type = ty | |
137 self.param_type = 'struct ' + ty + '*' | |
138 self.is_output = True | |
139 self.is_struct = True | |
140 self.is_extensible = False | |
141 return self | |
142 | |
143 def OutFixedStructArray(self, ty, size): | |
144 self.base_type = ty | |
145 self.param_type = 'struct ' + ty + '*' | |
146 self.size = size | |
147 self.is_array = True | |
148 self.is_output = True | |
149 return self | |
150 | |
151 # Declares that it is valid to pass a null pointer. | |
152 def Optional(self): | |
153 assert not self.IsPassedByValue() | |
154 self.is_optional = True | |
155 return self | |
156 | |
157 def AlwaysWritten(self): | |
158 assert self.is_output, self | |
159 self.is_always_written = True | |
160 return self | |
161 | |
162 def IsScalar(self): | |
163 return not self.is_array and not self.is_struct | |
164 | |
165 def IsPassedByValue(self): | |
166 return not self.is_output and self.IsScalar() | |
OLD | NEW |