summaryrefslogtreecommitdiffstats
path: root/ppapi/generators/idl_c_proto.py
blob: 145eff1dc5ec35a69c30cd567499a3d754914bf1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#!/usr/bin/python
#
# Copyright (c) 2011 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

""" Generator for C style prototypes and definitions """

import glob
import os
import sys

from idl_log import ErrOut, InfoOut, WarnOut
from idl_node import IDLAttribute, IDLAst, IDLNode
from idl_option import GetOption, Option, ParseOptions
from idl_outfile import IDLOutFile
from idl_parser import IDLParser, ParseFiles


#
# 'C' style parameter and return styles
#

# Normal parameters (int8_t, etc...)
NormalType = {
  'in': '$TYPEREF$',
  'inout': '$TYPEREF$*',
  'out': '$TYPEREF$*',
  'store': '$TYPEREF$',
  'return': '$TYPEREF$'
}

# Enum uses the enum's name, except when storing.
EnumType = {
  'in': '$TYPEREF$',
  'inout': '$TYPEREF$*',
  'out': '$TYPEREF$*',
  'store': 'int',
  'return': '$TYPEREF$'
}

# A mem_t is a 'void *' to memory.
MemPtrType = {
  'in': 'const void*',
  'inout': 'void*',
  'out': 'void*',
  'store': 'void*',
  'return': 'void*'
}

# A str_t is a string pointer 'char *'.
CharPtrType = {
  'in': 'const char*',
  'inout': 'char*',
  'out': 'char*',
  'return': 'char*',
  'store': 'char*'
}

# A function pointer type
FuncType = {
  'in': '$TYPEREF$',
  'inout': '$TYPEREF$',
  'out': '$TYPEREF$',
  'return': '$TYPEREF$',
  'store': '$TYPEREF$'
}

# A 'struct' is passed by pointer.
StructType = {
  'in': 'const $TYPEREF$*',
  'inout': '$TYPEREF$*',
  'out': '$TYPEREF$*',
  'return': '$TYPEREF$*',
  'store': '$TYPEREF$'
}

# A 'void' does not have a parameter or storage type.
VoidType = {
  'in': None,
  'inout': None,
  'out': None,
  'return': 'void',
  'store': None
}

TypeMap = {
  'func': FuncType,
  'enum': EnumType,
  'mem_t': MemPtrType,
  'str_t': CharPtrType,
  'struct': StructType,
  'void': VoidType
}


# Place the individual 'normal' types in the type map.
for name in ['int8_t', 'int16_t' ,'int32_t', 'int64_t', 'uint8_t', 'uint16_t',
             'uint32_t', 'uint64_t', 'double_t', 'float_t', 'handle_t', 'bool']:
  TypeMap[name] = NormalType



# Return the name of the node with suffix/prefix if availible.
def GetName(node, **keyargs):
  prefix = keyargs.get('prefix','')
  suffix = keyargs.get('suffix', '')
  named = keyargs.get('named', True)
  name = node.name
  if not named: name = ''
  return '%s%s%s' % (prefix, name, suffix)


# Return the array specification of the object.
def GetArrayspec(node, name):
  assert(node.cls == 'Array')
  out = ''
  fixed = node.GetProperty('FIXED')
  if fixed:
    out = '%s[%s]' % (name, fixed)
  else:
    out = '*%s' % name
  # Add children
  for child in node.GetListOf('Array'):
    out += GetArrayspec(child, name)
  return out


# Return the <name>[<size>] form of the node.
def GetNameArray(node, **keyargs):
  name = GetName(node, **keyargs)
  for child in node.GetListOf('Array'):
    name = GetArrayspec(child, name)
  return name


def GetRootType(node):
  root = node
  while root.typeinfo:
    typeinfo = root.typeinfo
    if root.GetOneOf('Callspec'):
      return TypeMap['func']
    root = root.typeinfo
  return TypeMap[root.name]


# Get the passing form of the parameter.
def GetParamType(node, **keyargs):
  typedata = GetRootType(node)
  if node.GetProperty('in'):
    return node.Replace(typedata['in'])
  if node.GetProperty('out'):
    return node.Replace(typedata['out'])
  if node.GetProperty('inout'):
    return node.Replace(typedata['inout'])
  return node.GetProperty('TYPEREF')


# GetParam
#
# A Param signature is different than that of a Member because a Member is
# stored in the structure as declared, but the Param's signature is affected
# by how it gets passed and it's extended attributes like IN, OUT, and INOUT.
#
def GetParam(node, **keyargs):
  # This may be a function.
  callnode = node.GetOneOf('Callspec')

  typeref  = GetParamType(node, **keyargs)
  name = GetNameArray(node, **keyargs)
  if callnode:
    arglist = GetParamList(callnode, **keyargs)
    return '%s (*%s)(%s)' % (typeref, name, ', '.join(arglist))
  return '%s %s' % (typeref, name)


# GetParamList
def GetParamList(node, **keyargs):
  assert(node.cls == 'Callspec')
  out = []
  for child in node.GetListOf('Param'):
    out.append(GetParam(child, **keyargs))
  return out


# DefineSignature
def DefineSignature(node, **keyargs):
  # This may be a function.
  callnode = node.GetOneOf('Callspec')

  typeref  = node.GetProperty('TYPEREF')
  name = GetNameArray(node, **keyargs)
  if callnode:
    arglist = GetParamList(callnode, **keyargs)
    return '%s (*%s)(%s)' % (typeref, name, ', '.join(arglist))
  return '%s %s' % (typeref, name)

# Define a Member (or Function) in an interface.
def DefineMember(node, **keyargs):
  return '%s;\n' % DefineSignature(node, **keyargs)

# Define an Typedef.
def DefineTypedef(node, **keyargs):
  return 'typedef %s;\n' % DefineSignature(node, **keyargs)

# Define an Enum.
def DefineEnum(node, **keyargs):
  out = 'enum %s {' % GetName(node, **keyargs)

  enumlist = []
  for child in node.GetListOf('EnumItem'):
    value = child.GetProperty('VALUE')
    enumlist.append('  %s = %s' % (GetName(child), value))
  return '%s\n%s\n};\n' % (out, ',\n'.join(enumlist))

# Define a Struct.
def DefineStruct(node, **keyargs):
  out = 'struct %s {\n' % (GetName(node, **keyargs))

  # Generate Member Functions
  for child in node.GetListOf('Function'):
    out += '  %s' % DefineMember(child, **keyargs)

  # Generate Member Variables
  for child in node.GetListOf('Member'):
    out += '  %s' % DefineMember(child, **keyargs)
  out += '};'
  return out


# Define a top level object.
def Define(node, **keyargs):
  declmap = {
    'Enum' : DefineEnum,
    'Interface' : DefineStruct,
    'Struct' : DefineStruct,
    'Typedef' : DefineTypedef,
  }

  func = declmap.get(node.cls)
  if not func:
    ErrLog.Log('Failed to define %s named %s' % (node.cls, node.name))
  out = func(node, **keyargs)

  tab = ''
  for i in range(keyargs.get('tabs', 0)):
    tab += '  '

  return ('%s\n' % tab).join(out.split('\n')) + '\n'


# Clean a string representing an object definition and return then string
# as a single space delimited set of tokens.
def CleanString(instr):
  instr = instr.strip()
  instr = instr.split()
  return ' '.join(instr)


# Test a file, by comparing all it's objects, with their comments.
def TestFile(filenode):
  errors = 0
  for node in filenode.Children()[2:]:
    if GetOption('verbose'):
      node.Dump(0, comments=True)

    instr = node.GetOneOf('Comment')
    instr = CleanString(instr.name)[3:-3]

    outstr = Define(node)
    outstr = CleanString(outstr)

    if instr != outstr:
      ErrOut.Log('Failed match of\n>>%s<<\n>>%s<<\nto:' % (instr, outstr))
      node.Dump(1, comments=True)
      errors += 1
  return errors



# Build and resolve the AST and compare each file individual.
def TestFiles(filenames):
  if not filenames:
    idldir = os.path.split(sys.argv[0])[0]
    idldir = os.path.join(idldir, 'test_cgen', '*.idl')
    filenames = glob.glob(idldir)

  filenames = sorted(filenames)
  ast_result = ParseFiles(filenames)

  total_errs = 0
  for filenode in ast_result.out.GetListOf('File'):
    errs = TestFile(filenode)
    if errs:
      ErrOut.Log('%s test failed with %d error(s).' % (filenode.name, errs))
      total_errs += errs

  if total_errs:
    ErrOut.Log('Failed generator test.')
  else:
    InfoOut.Log('Passed generator test.')
  return total_errs


def Main(args):
  filenames = ParseOptions(args)
  return TestFiles(filenames)

if __name__ == '__main__':
  sys.exit(Main(sys.argv[1:]))