{"id":248,"date":"2013-11-05T12:52:18","date_gmt":"2013-11-05T17:52:18","guid":{"rendered":"http:\/\/blog.mozilla.org\/nfroyd\/?p=248"},"modified":"2013-11-05T12:52:18","modified_gmt":"2013-11-05T17:52:18","slug":"ipdl-syntax-changes-for-types-coming-from-c","status":"publish","type":"post","link":"https:\/\/blog.mozilla.org\/nfroyd\/2013\/11\/05\/ipdl-syntax-changes-for-types-coming-from-c\/","title":{"rendered":"ipdl syntax changes for types coming from C++"},"content":{"rendered":"<p>Over the weekend, I landed <a href=\"https:\/\/bugzilla.mozilla.org\/show_bug.cgi?id=918651\">bug 918651<\/a>, which changes the syntax for how you inform the IPDL compiler about types defined in C++.\u00a0 Previously, you did:<\/p>\n<pre>include \"mozilla\/HeaderFile.h\";\r\n...\r\nusing typeFromHeaderFile;\r\n...<\/pre>\n<p>The <tt>using<\/tt> declaration informs the IPDL compiler that <tt>typeFromHeaderFile<\/tt> may appear in places types can normally appear.\u00a0 The <tt>include<\/tt> directive is so the generated headers know what to <code>#include<\/code> for the C++ compiler to be informed about <tt>typeFromHeaderFile<\/tt>.<\/p>\n<p>This scheme has a couple of drawbacks:<\/p>\n<ul>\n<li>The header files from the <tt>include<\/tt> directives aren&#8217;t connected to the <tt>using<\/tt> declarations in any way.\u00a0 Those headers might only include the relevant type(s) incidentally, which doesn&#8217;t help in <a href=\"https:\/\/bugzilla.mozilla.org\/show_bug.cgi?id=785103\">unraveling Gecko&#8217;s include dependencies<\/a>.<\/tt><\/tt><\/li>\n<li>The generated IPDL headers don&#8217;t necessarily need the full definition of typeFromHeaderFile.\u00a0 For structs or classes, the generated headers can get by with a simple forward declaration.\u00a0 The full definition is only needed in the generated source files.\u00a0 The above syntax, however, doesn&#8217;t enable any sort of forward declaration magic.<\/li>\n<\/ul>\n<p>To address both of these issues, the syntax for using declarations was changed.\u00a0 For structs, you should say:<\/p>\n<pre>using struct structFromHeaderFile from \"mozilla\/HeaderFile.h\"<\/pre>\n<p>The syntax for classes is similar:<\/p>\n<pre>using class classFromHeaderFile from \"mozilla\/HeaderFile.h\"<\/pre>\n<p>In these cases, the IPDL compiler will forward-declare the types where appropriate and only <code>#include<\/code> the header in the generated source files.\u00a0 Additionally, the compiler is intelligent enough to <code>#include<\/code> the header in the generated headers if it is required.  For instance, if there is a <tt>struct<\/tt> or a <tt>union<\/tt> defined in the header file that requires a struct or a class from a <tt>using<\/tt> declaration, the relevant header will be included in the generated header instead of the generated source file.<\/p>\n<p>Finally, if you need an enum type or a typedef, you should say:<\/p>\n<pre>using typeFromHeaderFile from \"mozilla\/HeaderFile.h\"<\/pre>\n<p>This case functions similarly to what we had before, except that the header file is now closely associated with the type; ideally, that will encourage people to use the correct header (i.e. the one that defines the type).\u00a0 While you are able to use this syntax with struct or class types, you should use the <tt>using struct<\/tt> or <tt>using class<\/tt> syntax, as appropriate, so that forward declarations are generated.<\/p>\n<p>There are still a few instances of <tt>include<\/tt> directives for C++ headers in IPDL files; those should be considered a bug, and the <tt>include<\/tt> directive for C++ headers should not normally be needed going forward.<\/p>\n<p>This change didn&#8217;t completely address the original issue of the bug (touching headers in <code>gfx\/<\/code> causes source files in <code>netwerk\/<\/code> to rebuild), but it moved us a lot closer to fixing those sorts of issues.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Over the weekend, I landed bug 918651, which changes the syntax for how you inform the IPDL compiler about types defined in C++.\u00a0 Previously, you did: include &#8220;mozilla\/HeaderFile.h&#8221;; &#8230; using typeFromHeaderFile; &#8230; The using declaration informs the IPDL compiler that typeFromHeaderFile may appear in places types can normally appear.\u00a0 The include directive is so the [&hellip;]<\/p>\n","protected":false},"author":320,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.mozilla.org\/nfroyd\/wp-json\/wp\/v2\/posts\/248"}],"collection":[{"href":"https:\/\/blog.mozilla.org\/nfroyd\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.mozilla.org\/nfroyd\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.mozilla.org\/nfroyd\/wp-json\/wp\/v2\/users\/320"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.mozilla.org\/nfroyd\/wp-json\/wp\/v2\/comments?post=248"}],"version-history":[{"count":0,"href":"https:\/\/blog.mozilla.org\/nfroyd\/wp-json\/wp\/v2\/posts\/248\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.mozilla.org\/nfroyd\/wp-json\/wp\/v2\/media?parent=248"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.mozilla.org\/nfroyd\/wp-json\/wp\/v2\/categories?post=248"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.mozilla.org\/nfroyd\/wp-json\/wp\/v2\/tags?post=248"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}