-
-
Notifications
You must be signed in to change notification settings - Fork 69
CppConversion
Visual D can help you converting your C++ code base to D. Even though it is capable of some quite extensive reorganization of the code, don't expect to get compilable and runnable code out of the box. Helping the converter with additional replacement rules and manual additional editing of the result might be necessary.
The conversion consists of a number of steps:
- convert text into a stream of tokens with preceding white space or comments - convert/expand preprocessor directives, so that the text is no longer a mixture of two languages. Conditionals are replaced by constructs that should fit into a tree hierarchy similar to "static if" in D. - apply user specified replacement rules (pre) - parse the text with a (slightly sloppy) C++ parser into a syntax tree - perform a number of transformations on the tree - apply user specified replacement rules (post) - write output including adding a prefix to D keywords that are not C++ keywords
You can invoke the C++ conversion wizard from the Visual D menu.
Options:
- Convert: Input files/Current document/Current selection. When selecting "Input files", a whole source code base can be converted, writing files to the output directory. Otherwise, the text in the editor is replaced. If you have not changed the text and selection after the last conversion, redoing the conversion will use the initial code to better allow tweaking options.
- Input dir: base directory of input file specifications - Files and directories: filename pattern to include into conversion, prefix with '+' to recurse into subdirectories, prefix with '-' to exclude files from the list of files specified so far. - Source code header: header to write into output files. The identifier MODULENAME is replaced with the module name of the generated D file. If it is not specified in the header, a module statement will be prepended to it. - Output dir: the directory where to write output files to. - Write intermediate files: To debug problems during the conversion, intermediate files can be written to sub directories "pass1" (after the proprocessor handling) and "pass2" (after the code reorganization, but before the final replacements).
- Preprocessor expansions: only the preprocessor identifiers given here will be expanded. All other preprocessor directives are translated to corresponding D code. If no assignment is given, the first definition within the input source code is used. If preprocessor conditionals are expanded, only the active code path is written to the output file. - Version conditionals: normally, preprocessor conditionals are translated to "static if" conditions, but if an identifier is specified here, version conditionals are used. - Pre and post token replacements: some constructs are not parsable or don't convert well. With the token replacements you can inject some replacement rules into the conversion process. The replacement follows the [wiki:Tour/TokenReplace] functionality. The syntax of the rule is
If you want to span the rule across multiple lines, use the \ line splicing mechanism. - Value/reference types: sometimes the parser or the converter stumbles across unknown identifiers that might be types. You can help it specifying them here. Value or reference semantics determine whether an indirection is removed from pointer declarations. - Keyword prefix: identifiers that are keywords in D, but not in C++, are prefixed with the given text. - Package prefix: module names are prefixed with the given package identifier - Load/Save: save and restore the current dialog settings - Convert: start the conversion
When using the converter on the current selection (there is also a command VisualD.!ConvertSelection that you can assign to a keyboard shortcut) you should select complete declarations, e.g. variable or class declarations and functions. Otherwise the parser will stumble.
This tool is the result of generalizing an attempt to convert the dmd front end code to D, so please don't be surprised to find some unexpected special casings from time to time. The result of that attempt was that the converted code was parsable, but did not pass all semantic stages.
There is also a command line version of this wizard available in the downloads folder: cpp2d.exe
You can pass a config file to the converter that has been saved by the wizard dialog.
If you pass files to the program, file specifications in the configuration file are ignored.
See here for an example of a configuration file to get started on converting the dmd front end.
Here are some examples of what the converter can do for you:
Move member and field implementation into class declaration.
Convert constructor and call super constructor
Split declarations for variables with different types
Add enumerator type to constants of named enumerator values
Simple template translations
Preprocessor macros are converted to enum, alias or templates
Preprocessor conditionals are converted to static if or can be removed when specified in the "expansions" field
It can deal with a number of complicated uses of preprocessor conditionals, but they can get much more difficult to read.