1
1
#!/usr/bin/env python
2
2
# header.py - Generate C++ header files from IDL.
4
# ***** BEGIN LICENSE BLOCK *****
5
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
7
# The contents of this file are subject to the Mozilla Public License Version
8
# 1.1 (the "License"); you may not use this file except in compliance with
9
# the License. You may obtain a copy of the License at
10
# http://www.mozilla.org/MPL/
12
# Software distributed under the License is distributed on an "AS IS" basis,
13
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14
# for the specific language governing rights and limitations under the
17
# The Original Code is mozilla.org code.
19
# The Initial Developer of the Original Code is
21
# Portions created by the Initial Developer are Copyright (C) 2008
22
# the Initial Developer. All Rights Reserved.
25
# Benjamin Smedberg <benjamin@smedbergs.us>
27
# Alternatively, the contents of this file may be used under the terms of
28
# either of the GNU General Public License Version 2 or later (the "GPL"),
29
# or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30
# in which case the provisions of the GPL or the LGPL are applicable instead
31
# of those above. If you wish to allow use of your version of this file only
32
# under the terms of either the GPL or the LGPL, and not to allow others to
33
# use your version of this file under the terms of the MPL, indicate your
34
# decision by deleting the provisions above and replace them with the notice
35
# and other provisions required by the GPL or the LGPL. If you do not delete
36
# the provisions above, a recipient may use your version of this file under
37
# the terms of any one of the MPL, the GPL or the LGPL.
39
# ***** END LICENSE BLOCK *****
4
# This Source Code Form is subject to the terms of the Mozilla Public
5
# License, v. 2.0. If a copy of the MPL was not distributed with this
6
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
41
8
"""Print a C++ header file for the IDL files specified on the command line"""
84
51
return ", ".join(l)
86
53
def attributeAsNative(a, getter):
87
scriptable = a.isScriptable() and "NS_SCRIPTABLE " or ""
88
54
deprecated = a.deprecated and "NS_DEPRECATED " or ""
89
params = {'scriptable': scriptable,
90
'deprecated': deprecated,
55
params = {'deprecated': deprecated,
91
56
'returntype': attributeReturnType(a, 'NS_IMETHOD'),
92
57
'binaryname': attributeNativeName(a, getter),
93
58
'paramlist': attributeParamlist(a, getter)}
94
return "%(deprecated)s%(scriptable)s%(returntype)s %(binaryname)s(%(paramlist)s)" % params
59
return "%(deprecated)s%(returntype)s %(binaryname)s(%(paramlist)s)" % params
96
61
def methodNativeName(m):
97
62
return m.binaryname is not None and m.binaryname or firstCap(m.name)
111
76
def methodAsNative(m):
112
scriptable = m.isScriptable() and "NS_SCRIPTABLE " or ""
114
return "%s%s %s(%s)" % (scriptable,
115
methodReturnType(m, 'NS_IMETHOD'),
117
paramlistAsNative(m))
77
return "%s %s(%s)" % (methodReturnType(m, 'NS_IMETHOD'),
119
81
def paramlistAsNative(m, empty='void'):
120
82
l = [paramAsNative(p) for p in m.params]
123
85
l.append("JSContext* cx")
125
87
if m.optional_argc:
126
l.append('PRUint8 _argc')
88
l.append('uint8_t _argc')
128
90
if not m.notxpcom and m.realtype.name != 'void':
129
91
l.append(paramAsNative(xpidl.Param(paramtype='out',
139
101
return ", ".join(l)
141
103
def paramAsNative(p):
142
if p.paramtype == 'in':
145
typeannotate = ' NS_%sPARAM' % p.paramtype.upper()
147
return "%s%s%s" % (p.nativeType(),
104
return "%s%s" % (p.nativeType(),
151
107
def paramlistNames(m):
152
108
names = [p.name for p in m.params]
214
175
if idl.needsJSTypes():
215
176
fd.write(jspubtd_include)
178
# Include some extra files if any attributes are infallible.
179
for iface in [p for p in idl.productions if p.kind == 'interface']:
180
for attr in [m for m in iface.members if isinstance(m, xpidl.Attribute)]:
182
fd.write(infallible_includes)
218
186
fd.write(header_end)
295
attr_infallible_tmpl = """\
296
inline %(realtype)s%(nativename)s(%(args)s)
299
mozilla::DebugOnly<nsresult> rv = %(nativename)s(%(argnames)s&result);
300
MOZ_ASSERT(NS_SUCCEEDED(rv));
327
305
def write_interface(iface, fd):
328
306
if iface.namemap is None:
329
307
raise Exception("Interface was not resolved.")
354
332
fd.write(" /* %s */\n" % a.toIDL());
356
334
fd.write(" %s = 0;\n" % attributeAsNative(a, True))
336
fd.write(attr_infallible_tmpl %
337
{'realtype': a.realtype.nativeType('in'),
338
'nativename': attributeNativeName(a, getter=True),
339
'args': '' if not a.implicit_jscontext else 'JSContext* cx',
340
'argnames': '' if not a.implicit_jscontext else 'cx, '})
357
342
if not a.readonly:
358
343
fd.write(" %s = 0;\n" % attributeAsNative(a, False))
390
375
if not foundcdata:
391
376
fd.write("NS_NO_VTABLE ")
393
if iface.attributes.scriptable:
394
fd.write("NS_SCRIPTABLE ")
395
378
if iface.attributes.deprecated:
396
379
fd.write("MOZ_DEPRECATED ")
397
380
fd.write(iface.name)
505
488
print >>sys.stderr, "--regen useless without --cachedir"
506
489
# Delete the lex/yacc files. Ply is too stupid to regenerate them
508
deadfiles = os.path.join(options.cachedir, "*.py*")
509
for filename in glob.glob(deadfiles):
491
for fileglobs in [os.path.join(options.cachedir, f) for f in ["xpidllex.py*", "xpidlyacc.py*"]]:
492
for filename in glob.glob(fileglobs):
513
495
# Instantiate the parser.
514
496
p = xpidl.IDLParser(outputdir=options.cachedir)