3 # This Source Code Form is subject to the terms of the Mozilla Public
4 # License, v. 2.0. If a copy of the MPL was not distributed with this
5 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 import mozpack
.path
as mozpath
11 from xpidl
import xpidl
13 # Load the webidl configuration file.
17 mozpath
.join(buildconfig
.topsrcdir
, "dom", "bindings", "Bindings.conf")
21 webidlconfig
= glbl
["DOMInterfaces"]
23 # Instantiate the parser.
27 def findIDL(includePath
, interfaceFileName
):
29 path
= mozpath
.join(d
, interfaceFileName
)
30 if os
.path
.exists(path
):
33 "No IDL file found for interface %s "
34 "in include path %r" % (interfaceFileName
, includePath
)
38 def loadEventIDL(parser
, includePath
, eventname
):
39 eventidl
= "nsIAccessible%s.idl" % eventname
40 idlFile
= findIDL(includePath
, eventidl
)
41 idl
= p
.parse(open(idlFile
).read(), idlFile
)
42 idl
.resolve(includePath
, p
, webidlconfig
)
47 def __init__(self
, filename
):
49 exec(open(filename
).read(), config
)
50 self
.simple_events
= config
.get("simple_events", [])
54 return str[0].upper() + str[1:]
57 def writeAttributeParams(a
):
58 return "%s a%s" % (a
.realtype
.nativeType("in"), firstCap(a
.name
))
61 def print_header_file(fd
, conf
, incdirs
):
64 fd
.write("/* THIS FILE IS AUTOGENERATED - DO NOT EDIT */\n")
66 "#ifndef _mozilla_a11y_generated_AccEvents_h_\n"
67 "#define _mozilla_a11y_generated_AccEvents_h_\n\n"
69 fd
.write('#include "nscore.h"\n')
70 fd
.write('#include "nsCOMPtr.h"\n')
71 fd
.write('#include "nsCycleCollectionParticipant.h"\n')
72 fd
.write('#include "nsString.h"\n')
73 for e
in conf
.simple_events
:
74 fd
.write('#include "nsIAccessible%s.h"\n' % e
)
75 for e
in conf
.simple_events
:
76 idl
, idl_path
= loadEventIDL(p
, incdirs
, e
)
77 idl_paths
.add(idl_path
)
78 for iface
in filter(lambda p
: p
.kind
== "interface", idl
.productions
):
79 classname
= "xpcAcc%s" % e
80 baseinterfaces
= interfaces(iface
)
82 fd
.write("\nclass %s final : public %s\n" % (classname
, iface
.name
))
86 attributes
= allAttributes(iface
)
87 args
= map(writeAttributeParams
, attributes
)
88 fd
.write(" %s(%s) :\n" % (classname
, ", ".join(args
)))
92 initializers
.append("m%s(a%s)" % (firstCap(a
.name
), firstCap(a
.name
)))
93 fd
.write(" %s\n {}\n\n" % ", ".join(initializers
))
94 fd
.write(" NS_DECL_CYCLE_COLLECTING_ISUPPORTS\n")
95 fd
.write(" NS_DECL_CYCLE_COLLECTION_CLASS(%s)\n" % (classname
))
97 for iface
in filter(lambda i
: i
.name
!= "nsISupports", baseinterfaces
):
98 fd
.write(" NS_DECL_%s\n" % iface
.name
.upper())
100 fd
.write("\nprivate:\n")
101 fd
.write(" ~%s() {}\n\n" % classname
)
103 fd
.write(" %s\n" % attributeVariableTypeAndName(a
))
111 def interfaceAttributeTypes(idl
):
112 ifaces
= filter(lambda p
: p
.kind
== "interface", idl
.productions
)
115 ifaceAttributes
= allAttributes(i
)
116 attributes
.extend(ifaceAttributes
)
117 ifaceAttrs
= filter(lambda a
: a
.realtype
.nativeType("in").endswith("*"), attributes
)
118 return map(lambda a
: a
.realtype
.nativeType("in").strip(" *"), ifaceAttrs
)
121 def print_cpp(idl
, fd
, conf
, eventname
):
122 for p
in idl
.productions
:
123 if p
.kind
== "interface":
124 write_cpp(eventname
, p
, fd
)
127 def print_cpp_file(fd
, conf
, incdirs
):
129 fd
.write("/* THIS FILE IS AUTOGENERATED - DO NOT EDIT */\n\n")
130 fd
.write('#include "xpcAccEvents.h"\n')
133 for e
in conf
.simple_events
:
134 if e
not in includes
:
135 includes
.append(("nsIAccessible%s" % e
))
138 for e
in conf
.simple_events
:
139 idl
, idl_path
= loadEventIDL(p
, incdirs
, e
)
140 idl_paths
.add(idl_path
)
141 types
.extend(interfaceAttributeTypes(idl
))
144 fd
.write('#include "%s.h"\n' % c
)
147 for e
in conf
.simple_events
:
148 idl
, idl_path
= loadEventIDL(p
, incdirs
, e
)
149 idl_paths
.add(idl_path
)
150 print_cpp(idl
, fd
, conf
, e
)
155 def attributeVariableTypeAndName(a
):
156 if a
.realtype
.nativeType("in").endswith("*"):
159 % (a
.realtype
.nativeType("in").strip("* "), firstCap(a
.name
))
161 elif a
.realtype
.nativeType("in").count("nsAString"):
162 l
= ["nsString m%s;" % firstCap(a
.name
)]
163 elif a
.realtype
.nativeType("in").count("nsACString"):
164 l
= ["nsCString m%s;" % firstCap(a
.name
)]
166 l
= ["%sm%s;" % (a
.realtype
.nativeType("in"), firstCap(a
.name
))]
170 def writeAttributeGetter(fd
, classname
, a
):
171 fd
.write("NS_IMETHODIMP\n")
172 fd
.write("%s::Get%s(" % (classname
, firstCap(a
.name
)))
173 if a
.realtype
.nativeType("in").endswith("*"):
175 "%s** a%s" % (a
.realtype
.nativeType("in").strip("* "), firstCap(a
.name
))
177 elif a
.realtype
.nativeType("in").count("nsAString"):
178 fd
.write("nsAString& a%s" % firstCap(a
.name
))
179 elif a
.realtype
.nativeType("in").count("nsACString"):
180 fd
.write("nsACString& a%s" % firstCap(a
.name
))
182 fd
.write("%s*a%s" % (a
.realtype
.nativeType("in"), firstCap(a
.name
)))
185 if a
.realtype
.nativeType("in").endswith("*"):
186 fd
.write(" NS_IF_ADDREF(*a%s = m%s);\n" % (firstCap(a
.name
), firstCap(a
.name
)))
187 elif a
.realtype
.nativeType("in").count("nsAString"):
188 fd
.write(" a%s = m%s;\n" % (firstCap(a
.name
), firstCap(a
.name
)))
189 elif a
.realtype
.nativeType("in").count("nsACString"):
190 fd
.write(" a%s = m%s;\n" % (firstCap(a
.name
), firstCap(a
.name
)))
192 fd
.write(" *a%s = m%s;\n" % (firstCap(a
.name
), firstCap(a
.name
)))
193 fd
.write(" return NS_OK;\n")
197 def interfaces(iface
):
200 interfaces
.append(iface
)
201 iface
= iface
.idl
.getName(xpidl
.TypeId(iface
.base
), iface
.location
)
202 interfaces
.append(iface
)
207 def allAttributes(iface
):
209 for i
in interfaces(iface
):
210 attrs
= filter(lambda m
: isinstance(m
, xpidl
.Attribute
), i
.members
)
211 attributes
.extend(attrs
)
216 def write_cpp(eventname
, iface
, fd
):
217 classname
= "xpcAcc%s" % eventname
218 attributes
= allAttributes(iface
)
219 ccattributes
= filter(
220 lambda m
: m
.realtype
.nativeType("in").endswith("*"), attributes
222 fd
.write("NS_IMPL_CYCLE_COLLECTION(%s" % classname
)
223 for c
in ccattributes
:
224 fd
.write(", m%s" % firstCap(c
.name
))
227 fd
.write("NS_IMPL_CYCLE_COLLECTING_ADDREF(%s)\n" % classname
)
228 fd
.write("NS_IMPL_CYCLE_COLLECTING_RELEASE(%s)\n\n" % classname
)
230 fd
.write("NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(%s)\n" % classname
)
231 for baseiface
in interfaces(iface
):
232 fd
.write(" NS_INTERFACE_MAP_ENTRY(%s)\n" % baseiface
.name
)
233 fd
.write("NS_INTERFACE_MAP_END\n\n")
236 writeAttributeGetter(fd
, classname
, a
)
239 def get_conf(conf_file
):
240 conf
= Configuration(conf_file
)
242 mozpath
.join(buildconfig
.topsrcdir
, "accessible", "interfaces"),
243 mozpath
.join(buildconfig
.topsrcdir
, "xpcom", "base"),
248 def gen_files(fd
, conf_file
):
250 conf
, inc_dir
= get_conf(conf_file
)
251 deps
.update(print_header_file(fd
, conf
, inc_dir
))
253 os
.path
.join(os
.path
.dirname(fd
.name
), "xpcAccEvents.cpp"), "w"
255 deps
.update(print_cpp_file(cpp_fd
, conf
, inc_dir
))