Skip to topic | Skip to bottom
Home
Projects
Projects.MetaGeneTodor1.1 - 03 Dec 2003 - 15:09 - FrancisMaes?topic end

Start of topic | Skip to actions
Metagene can be improved by several ways. We present here some ideas of extensions that could be useful. In its current version, it is easy to write toy-programs with Metagene. However, this tool remains difficult to use in real-scale applications. Most of the improvments suggested here should help in writing real meta-programs.

Type Checking

Initially Metagene was thinked to provide a real type checking mechanism, which is inexistant in usual C++ metaprogramming. This goal has not been fully reached. Here is the principle of what should be done to support type checking.

1. Read the Metagene input program

2. Translate it into standart OCaml code. Therefore, one possibility is to replace C++ types and C++ primitives by strings.

3. Typecheck to OCaml code, using the OCaml compiler.

4. If type checking passed, generate the C++ meta-program.

New possibilities

Thanks to type-checking, it will be impossible to translate an invalid Metagene program. When a C++ meta-program produces and error, the message given by the C++ compiler is often unreadable. Thanks to OCaml type-checking we would have completly readable errors.

Generating an OCaml version of the program has another major advandage: it becomes possible to use the OCaml interpretor during the development of Metagene programs. Since C++ meta-programs are known to be difficult to debug, this would offer a big help to Metagene developpers. Ideally executing the OCaml version of our programs would print the generated C++ code.

Problems to be solved

The program that will be typechecked by the OCaml compiled is not the Metagene one. Indead when translating to OCaml we lose some informations. The objective is to find an intelligent way to do the Metagene -> OCaml translation. On one hand we would like to lose a minimum of informations during this conversion. On the other hand it should be great that the execution of the OCaml program produces something near to the execution of the final C++ meta-program.

C++ Classes

Currently, Metagene supports two types designed for C++ meta-programming: C++ types and C++ primitives. In order to manipulate classes in the same way, we could add a new builtin type C++ class.

We could use the following syntax:

Types:      <t@ std::string @>
Primitives: <p@ int a, float b @ bool @ return a == (int)b; @>
Classes:    <c@ public BaseClass @
              public:
                 @self@() : data(51)    {}
                 ~@self@()              {std::cout << "destructor" << std::endl;}

                 virtual void method() {}
                 int data;
              @>

New possibilities

First of all, this would allow to include usual genericity in Metagene programs. A generic vector would thus be a function taking a C++ type and returning a C++ class:

let vector T = <c@ /* no base class */ @ 
                 public:
                     T& operator [](unsigned i)              {return _data[i];}
                     const T& operator [](unsigned i) const  {return _data[i];}
                     /* ... */
                 private:
                     T* _data;
                     /* ... */
               @>

Secondly, thanks to values seen as C++ classes it would be possible to write functions from class to class.

This way, it would be possible to write Metagene functions from classes to classes. Moreover this allows to express in an easy way conditionnal inheritance.

Ex:

let inherit2 class1 class2 = <c@ public class1, public class2 @ /* empty */ @>
let emptyclass = <c@ /* no base class */ @ /* no body */ @>
let inherit = List.fold_left inherit2 emptyclass

let myclass = inherit [ baseclass1;
                        get_base_class params;
                        if cond then baseclass3 else baseclass4 ]

Problems to be solved

  • In the previous example of vector, T cannot be simply inferred. We need to choose between three possibilities:

    • Parsing classes in order to extract typing info. In our example we want to see that T is a cxxtype. This solution seems complicated since it needs to do C++ parsing.

    • Explicit typing: in the example of vector, type T explicitly as cxxtype. Nothing to change in Metagene, but makes code longer. This seems to be reasonable.

    • Lazy typing, changes nothing: so vector's signature is 'a -> cxxclass'. One of the objectives of Metagene is to provide correct typing in our C++ meta-programs. This solution bring us back to nearly untyped function.

  • In order to declare constructors and destructors in our classes, we need their name. In general we do not have this name: so we require a special symbol (e.g. @self@) that will be handled by Metagene.

  • Classes need to be constructed with a quotation syntax, but we also would like to reference them with their name (as in the inheritance example).

  • Classes are also types. So their must be a way to convert a cxxclass into a cxxtype.

  • On one hand, this new feature allows to write any class template as a Metagene function. On the other hand we do not want Metagene to be everywhere in a project (we would use another language in this case: this is not the objective of Metagene). We thus need to find a right middle between these two extremums.

Exceptions

Objective Caml includes an efficient exceptions mechanism. Currently this mechanisms is not handled at all by Metagene. The objective is to study how to integrate exceptions in C++ meta-programs. Some static assert mechanisms have already been proposed (see static_assert Boost library).

New possibilities

Exceptions can be handled in two ways:

  • Handle exceptions in the OCaml version only (see Type Checking).

  • Handle exceptions in the C++ generated code. This seems much more complicated.

Problems to be solved

OCaml exceptions are much more evolved than the existing static asserts mechanisms: exceptions can be throwed, catched, transmited, etc... Several problems arise when trying to include a complete exception mechanism in C++ metaprograms. Any Metagene function can return its normal value or an exception. When exceptions are return, nothing else must be done: all nested functions must transmit the exception without doing their normal task. So, there are big chances that handling exceptions make the C++ generated code much longer.

Introspective data

Metagene could win a level of genericity by having introspective data. Imagine the following translation of a primitive:

let prim = <p@ int a, float b @ bool @ return a == (int)b; @>

struct prim {
  typedef List< int, List< float, EmptyList > > arguments_t;
  typedef bool                                  returntype_t;

  static bool value(int a, float b) {
     return a == (int)b;
  } 
};

We generate here two new C++ types: arguments_t and returntype_t. Such mechanisms can easily be extended to C++ classes.

New possibilities

Several new functions could be written with such additionnal informations. Examples:

let get_returntype : cxxprim -> cxxtype
let get_arguments : cxxprim -> cxxtype list
let get_methods : cxxclass -> cxxprim list
let get_baseclasses : cxxclass -> cxxclass list
...

Problems

  • Try a way to write the following function: let add_method : cxxclass -> cxxprim -> cxxclass.

  • The generated code will be much longer.

Information functions

The Boost package includes another library, which could be useful in Metagene programs: type_traits. This libary includes lots of information functions about C++ constructs, i.e. is_void, is_float, is_function, is_class, is_polymorphic, is_base_and_derived, is_convertible... This library could be encapsulated in a Metagene module having a clean interface, using Metagene types.

Example:

let is_base_and_derivate: cxxclass -> cxxclass -> bool
let is_convertible: cxxtype -> cxxtype -> bool

-- FrancisMaes? - 03 Dec 2003
to top


You are here: Projects > MetaGeneZone > MetaGeneTodo

to top

Copyright © 1999-2010 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback