Type system plug-ins [Was: Re: Coverity tool]

bearophile bearophileHUGS at lycos.com
Fri Feb 12 09:07:16 PST 2010

I've just found a nice article about this topic, "LCA: Static analysis with GCC plugins", by Jonathan Corbet:

Beside allowing plug-ins, they have done something else to GCC:
>The ability to attach attributes to objects in the compiled code makes it easy to pass hints through to later processing steps. The new pass manager brings a relatively modern structure to a compiler which did not originally have one. And the GIMPLE intermediate representation provides much of the rest of what's needed for code which needs to inspect other code.<

So now they are essentially extending the C++ type system using small JavaScript programs:
>Various other tools have been written. The final.js script (a dozen lines of code which can be seen on this page) looks for C++ methods tagged with the "final" attribute; any attempt to override those methods will result in a compilation error. It is, in other words, a port of the Java final keyword to C++. A checker which might be interesting in other environments - including the kernel - is flow.js, which can add a constraint that all exits from a function must flow through a specific label.<

The final.js code:

 * Helper function: returns true if a class is marked with the "final" attribute.
function isFinal(c) {
  if (!c.attributes)
    return false;
  for each (let a in c.attributes)
    if (a.name == 'user' && a.value == 'final')
      return true;
  return false;
function process_type(t) {
  if (t.bases)
    for each (let base in t.bases)
      if (isFinal(base.type))
        error("class " + t.name + " extends final class " + base.type.name, t.loc);

Then in C++ the syntax of final class is not nice:
class __attribute__((user("final"))) MyClass {

In D you can probably write the same thing as (D already has final, so this is just to compare):
@final class MyClass {

I think you can implement nonnullable references too (but you can't use the nice "?" syntax for nullables).

Now please give me five minutes to dream about doing something similar using Python for D :-) It can be even possible to use D to write those scripts, especially once types become more first class values in D.


More information about the Digitalmars-d mailing list