bioinfornatics
Joined: 22 Jun 2010 Posts: 90
|
Posted: Sat Oct 29, 2011 5:27 am Post subject: Patch bcdgen for D2 here |
|
|
I have do a patch with this one, build works fine
Code: |
diff -up bcd.gen/bcd/gen/bcdgen.d.fix bcd.gen/bcd/gen/bcdgen.d
--- bcd.gen/bcd/gen/bcdgen.d.fix 2011-10-29 13:25:34.855971101 +0200
+++ bcd.gen/bcd/gen/bcdgen.d 2011-10-29 04:27:43.335537615 +0200
@@ -1,24 +1,24 @@
/**
* Generate bindings for C[++] in D
- *
+ *
* Authors:
* Gregor Richards
* Tomas "MrSunshine" Wilhelmsson
- *
+ *
* License:
* Copyright (C) 2006, 2007 Gregor Richards
* Copyright (C) 2006 Tomas "MrSunshine" Wilhelmsson
- *
+ *
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
- *
+ *
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
- *
+ *
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
@@ -32,46 +32,43 @@ version (Windows) {
import std.path;
}
-import std.file;
+import std.file : mkdir, FileException;
import std.process;
-import std.stdio;
-import std.stream;
+import std.stdio : writefln, File;
import std.string;
-
import std.c.stdlib;
+import std.conv;
alias std.string.atoi atoi;
alias std.c.stdlib.free free;
alias std.process.system system;
private import bcd.gen.libxml2;
-extern (C) char* getenv(char*);
-
// some global variables (yay)
private {
/** The full path to the current file */
-char[] curFile;
+string curFile;
/** The include prefix */
-char[] incPrefix;
+string incPrefix;
/** The base directory of .h files */
-char[] baseDir;
+string baseDir;
/** The short name of the current .h file (no basename, no .h) */
-char[] shortName;
+string shortName;
/** The D namespace */
-char[] dNamespace;
+string dNamespace;
/** The C++ functions/variables to explicitly ignore */
-char[][] ignoreSyms;
+string[] ignoreSyms;
}
/** The D output */
-char[] dhead;
-char[] dtail;
+string dhead;
+string dtail;
/** The C[++] output */
-char[] cout;
+string cout;
private {
/** The base of the D namespace */
-char[] dNamespaceBase;
+string dNamespaceBase;
/** The class currently being processed */
-char[] curClass;
+string curClass;
/** Is the current class abstract? */
bool curClassAbstract;
/** Was a constructor made for the current class? */
@@ -90,35 +87,35 @@ bool outputEnumConst;
bool outputReflections;
/** Other BCD requirements */
bool polluteNamespace = false;
-char[][char[]] reqDependencies;
+string[string] reqDependencies;
/** The root to the XML tree */
xmlNode *gccxml = null;
/** Class currently being reflected into D */
-char[] curReflection;
+string curReflection;
/** The base of the class currently being reflected (in C++) */
-char[] curReflectionCBase;
+string curReflectionCBase;
/** The base of the class currently being reflected (in D) */
-char[] curReflectionDBase;
+string curReflectionDBase;
/** The initializer for the current reflection */
-char[] curReflectionInit;
+string curReflectionInit;
/** The C++ code for the class currently being reflected */
-char[] reflectionCode;
+string reflectionCode;
/** The C++ code to go after we close the class */
-char[] reflectionPostCode;
+string reflectionPostCode;
/** The functions that have already been reflected */
-bool[char[]] reflectedFunctions;
+bool[string] reflectedFunctions;
-char[][char[]] files;
+string[string] files;
}
-int main(char[][] args)
+int main(string[] args)
{
// figure out what gccxml to use based on the system
- char[] gccxmlExe;
+ string gccxmlExe;
version (Windows) {
- char[] dir, bname;
+ string dir, bname;
whereAmI(args[0], dir, bname);
gccxmlExe = dir ~ "\\gccxml\\gccxml_flags.exe";
} else {
@@ -158,74 +155,74 @@ int main(char[][] args)
return 1;
}
- char[] forcedImport;
- char[] templates;
-
+ string forcedImport;
+ string templates;
+
// set the globals
dNamespaceBase = "bcd.";
curFile = args[1];
version (Windows) {
// get*Name only works with \, but gccxml only works with /
- curFile = replace(curFile, "\\", "/");
- char[] backslashName = replace(args[1], "/", "\\");
- baseDir = replace(getDirName(backslashName), "\\", "/");
+ curFile = tr(curFile, "\\", "/");
+ string backslashName = tr(args[1], "/", "\\");
+ baseDir = tr(getDirName(backslashName), "\\", "/");
shortName = getBaseName(backslashName);
} else {
baseDir = getDirName(args[1]);
shortName = getBaseName(args[1]);
}
- if (find(shortName, '.') != -1) {
+ if (shortName != ".") {
shortName = getName(shortName);
}
shortName = safeName(shortName);
-
+
// parse other options
for (int i = 3; i < args.length; i++) {
if (args[i][0..2] == "-I") {
incPrefix = args[i][2..args[i].length];
-
+
} else if (args[i][0..2] == "-N") {
ignoreSyms ~= args[i][2..args[i].length];
-
+
} else if (args[i] == "-C") {
outputC = true;
-
+
} else if (args[i][0..2] == "-R") {
- char[] req = args[i][2..args[i].length];
- int eqloc = find(req, '=');
+ string req = args[i][2..args[i].length];
+ int eqloc = (req == "=") ? 1 : -1 ;
if (eqloc == -1) {
writefln("Argument %s not recognized.", args[i]);
continue;
}
reqDependencies[req[0..eqloc]] = req[eqloc + 1 .. req.length];
-
+
} else if (args[i] == "-A") {
outputAll = true;
-
+
} else if (args[i][0..2] == "-F") {
forcedImport ~= "public import " ~ args[i][2..args[i].length] ~ ";\n";
-
+
} else if (args[i][0..2] == "-T") {
- char[] temp = args[i][2..args[i].length];
+ string temp = args[i][2..args[i].length];
int count = 1, eqloc;
-
- eqloc = find(temp, '=');
+
+ eqloc = (temp == "=") ? 1 : -1 ;
if (eqloc != -1) {
- count = atoi(temp[eqloc + 1 .. temp.length]);
+ count = atoi(temp[eqloc + 1 .. temp.length].toStringz);
temp = temp[0..eqloc];
}
-
+
templates ~= temp ~ "<DReflectedClass";
-
+
for (int j = 1; i < count; i++)
templates ~= ", DReflectedClass";
-
+
templates ~= "> __IGNORE_" ~ temp ~ ";\n";
} else if (args[i] == "-P") {
polluteNamespace = true;
} else if (args[i] == "-E") {
outputEnumConst = true;
-
+
} else if (args[i] == "-r") {
outputReflections = true;
@@ -237,25 +234,25 @@ int main(char[][] args)
writefln("Argument %s not recognized.", args[i]);
}
}
-
+
// the header to attach to all generated files
- const char[] genhead = "/* THIS FILE GENERATED BY bcd.gen */\n";
-
+ const string genhead = "/* THIS FILE GENERATED BY bcd.gen */\n";
+
dNamespace = args[2];
-
+
// some buffers
dhead = genhead; // the D header (extern (C)'s)
dhead ~= "module " ~ dNamespaceBase ~ dNamespace ~ "." ~ shortName ~ ";\nalign(4):\n";
if (!outputC) dhead ~= "public import bcd.bind;\n";
dhead ~= forcedImport;
-
+
cout = genhead; // the C++ output (to name.cc)
cout ~= "#include <stdlib.h>\n";
cout ~= "#include <string.h>\n";
cout ~= "#include \"../bind.h\"\n";
cout ~= "#include \"" ~ incPrefix ~ getBaseName(args[1]) ~ "\"\n";
if (!outputC) cout ~= "extern \"C\" {\n";
-
+
// make the directories
try {
mkdir("bcd");
@@ -263,89 +260,92 @@ int main(char[][] args)
try {
mkdir("bcd/" ~ dNamespace);
} catch (FileException e) {} // ignore errors
-
+
// make the template file if requested
if (templates != "") {
- write("bcd/" ~ dNamespace ~ "/template_D.h",
+ std.file.write("bcd/" ~ dNamespace ~ "/template_D.h",
"#include \"../bind.h\"\n" ~
"#include \"" ~ incPrefix ~ getBaseName(args[1]) ~ "\"\n" ~
templates);
templates = "-include bcd/" ~ dNamespace ~ "/template_D.h ";
}
-
+
// gccxml options
- char[] gccxmlopts = templates ~
- toString(getenv(toStringz(outputC ? "CFLAGS" : "CXXFLAGS")));
-
+ string gccxmlopts = "";
+ if( outputC )
+ gccxmlopts = templates ~ to!(string)(getenv("CFLAGS".toStringz));
+ else
+ gccxmlopts = templates ~ to!(string)(getenv("CXXFLAGS".toStringz));
+
// preprocess it
if (system(gccxmlExe ~ " -E -dD " ~ gccxmlopts ~ " -o out.i " ~ args[1])) {
return 2;
}
-
+
parse_Defines();
-
+
// xml-ize it
if (system(gccxmlExe ~ " " ~ gccxmlopts ~
" -fxml=out.xml " ~ args[1])) {
return 2;
}
-
+
// then initialize libxml2
xmlDoc *doc = null;
xmlNode *rootElement = null;
-
+
xmlCheckVersion(20621); // the version bcdgen's XML was ported from
-
+
// and read it in
- doc = xmlReadFile("out.xml", null, 0);
-
+ doc = xmlReadFile("out.xml".dup.ptr, null, 0);
+
if (doc == null) {
writefln("Failed to parse the GCCXML-produced XML file!");
return 3;
}
-
+
// Get the root element node
gccxml = xmlDocGetRootElement(doc);
-
+
parse_GCC_XML();
-
+
xmlCleanupParser();
-
+
if (!outputC) cout ~= "}\n";
-
+
// write out the files
- write("bcd/" ~ dNamespace ~ "/" ~ shortName ~ ".d",
+ std.file.write("bcd/" ~ dNamespace ~ "/" ~ shortName ~ ".d",
dhead ~ dtail);
if (!outputC) {
- write("bcd/" ~ dNamespace ~ "/" ~ shortName ~ ".cc",
+ std.file.write("bcd/" ~ dNamespace ~ "/" ~ shortName ~ ".cc",
cout);
}
-
+
// get rid of the files we no longer need
if (templates != "") {
std.file.remove("bcd/" ~ dNamespace ~ "/template_D.h");
}
//std.file.remove("out.i");
//std.file.remove("out.xml");
-
+
return 0;
}
/**
* Is this numeric?
*/
-bool isNumeric(char[] str)
+bool isNumeric(string str)
{
bool hase, hasdot, ishex;
-
+
for (int i = 0; i < str.length; i++) {
char c = str[i];
if (c >= '0' && c <= '9') continue;
if (ishex &&
((c >= 'a' && c <= 'f') ||
(c >= 'A' && c <= 'F'))) continue;
-
-
+
+
if (c == 'e' || c == 'E') {
if (hase) return false;
hase = true;
@@ -364,16 +364,16 @@ bool isNumeric(char[] str)
return false;
}
}
-
+
return true;
}
/**
* Return a name that doesn't stomp on any D keywords
*/
-char[] safeName(char[] name)
+string safeName(string name)
{
- char[] ret = name[0..name.length];
+ string ret = name[0..name.length];
if (name == "alias" ||
name == "align" ||
name == "body" ||
@@ -382,7 +382,7 @@ char[] safeName(char[] name)
name == "function" ||
name == "in" ||
name == "int" ||
- name == "inout" ||
+ name == "ref" ||
name == "long" ||
name == "module" ||
name == "out" ||
@@ -397,28 +397,28 @@ char[] safeName(char[] name)
name == "with") {
ret ~= "_";
}
-
- ret = replace(ret, ".", "_");
- ret = replace(ret, "-", "_");
-
+
+ ret = tr(ret, ".", "_");
+ ret = tr(ret, "-", "_");
+
// drop any template info
- int tloc = find(ret, '<');
+ int tloc = ret == "<";
if (tloc != -1) {
ret = ret[0..tloc] ~ "_D";
}
-
+
return ret;
}
/**
* Return name unless it's anonymous, in which case return mangled
*/
-char[] getNName(xmlNode *node)
+string getNName(xmlNode *node)
{
- char[] name = toStringFree(xmlGetProp(node, "name"));
- if (name == "") name = toStringFree(xmlGetProp(node, "mangled"));
+ string name = toStringFree(xmlGetProp(node, "name".dup.ptr));
+ if (name == "") name = toStringFree(xmlGetProp(node, "mangled".dup.ptr));
if (name == "") {
- char[] id = toStringFree(xmlGetProp(node, "id"));
+ string id = toStringFree(xmlGetProp(node, "id".dup.ptr));
if (id != "")
name = "_BCD_" ~ id;
}
@@ -426,12 +426,12 @@ char[] getNName(xmlNode *node)
}
/**
- * Convert a char * to a char[] and free it
+ * Convert a char * to a string and free it
*/
-char[] toStringFree(char* from)
+string toStringFree(char* from)
{
- char[] ret;
- ret ~= toString(from);
+ string ret;
+ ret ~= to!(string)(from);
free(from);
return ret;
}
@@ -441,8 +441,8 @@ char[] toStringFree(char* from)
*/
char *getMangled(xmlNode *node)
{
- char *mangled = xmlGetProp(node, "mangled");
- if (!mangled && outputC) mangled = xmlGetProp(node, "name");
+ char *mangled = xmlGetProp(node, "mangled".dup.ptr);
+ if (!mangled && outputC) mangled = xmlGetProp(node, "name".dup.ptr);
return mangled;
}
@@ -451,8 +451,8 @@ char *getMangled(xmlNode *node)
*/
char *getDemangled(xmlNode *node)
{
- char *demangled = xmlGetProp(node, "demangled");
- if (!demangled && outputC) demangled = xmlGetProp(node, "name");
+ char *demangled = xmlGetProp(node, "demangled".dup.ptr);
+ if (!demangled && outputC) demangled = xmlGetProp(node, "name".dup.ptr);
return demangled;
}
@@ -462,15 +462,15 @@ char *getDemangled(xmlNode *node)
bool parseThis(xmlNode *node, bool allowNameless = false)
{
// only parse it if it's in the file we're parsing and it's demanglable
- char* file = xmlGetProp(node, "file");
+ char* file = xmlGetProp(node, "file".dup.ptr);
char* demangled = getDemangled(node);
- char* incomplete = xmlGetProp(node, "incomplete");
+ char* incomplete = xmlGetProp(node, "incomplete".dup.ptr);
if (incomplete) free(incomplete);
-
+
if (file && (demangled || allowNameless) && !incomplete) {
- char[] sfile = toStringFree(file);
- char[] sdemangled = toStringFree(demangled);
-
+ string sfile = toStringFree(file);
+ string sdemangled = toStringFree(demangled);
+
// if it's not in a file we should be parsing, don't output it
if (outputAll) {
if (files[sfile].length <= baseDir.length ||
@@ -478,24 +478,24 @@ bool parseThis(xmlNode *node, bool allow
} else {
if (files[sfile] != curFile) return false;
}
-
+
// if it's builtin, don't do it
- char* name = xmlGetProp(node, "name");
+ char* name = xmlGetProp(node, "name".dup.ptr);
if (name) {
- char[] sname = toStringFree(name);
+ string sname = toStringFree(name);
if (sname.length >= 9 &&
sname[0..9] == "__builtin") {
return false;
}
}
-
+
// if access is private, don't do it
- char* access = xmlGetProp(node, "access");
+ char* access = xmlGetProp(node, "access".dup.ptr);
if (access &&
toStringFree(access) != "public") {
return false;
}
-
+
// if we were told to ignore it, do so
if (sdemangled.length >= 8 &&
sdemangled[0..8] == "__IGNORE") return false;
@@ -504,7 +504,7 @@ bool parseThis(xmlNode *node, bool allow
return false;
}
}
-
+
return true;
} else {
if (file) free(file);
@@ -518,12 +518,12 @@ bool parseThis(xmlNode *node, bool allow
*/
void parseMembers(xmlNode *node, bool inclass, bool types, bool reflection = false)
{
- char *members = xmlGetProp(node, "members");
+ char *members = xmlGetProp(node, "members".dup.ptr);
if (!members) return;
-
- char[] smembers = toStringFree(members);
- char[][] memberList = split(smembers);
-
+
+ string smembers = toStringFree(members);
+ string[] memberList = split(smembers);
+
// parse each member in the memberList
foreach (m; memberList) {
parse_GCC_XML_for(m, inclass, types, reflection);
@@ -537,27 +537,27 @@ void parse_GCC_XML()
{
xmlNode *curNode = null;
int xmlrret;
-
+
// first parse for files and fundamental types
for (curNode = gccxml.children; curNode; curNode = curNode.next) {
if (curNode.type == xmlElementType.XML_ELEMENT_NODE) {
- char[] nname = toString(curNode.name);
-
+ string nname = to!(string)(curNode.name);
+
//writefln(" %s", nname);
-
+
if (nname == "File") {
parse_File(curNode);
}
}
}
-
+
// then parse for namespaces, typedefs, enums (all of which can be top-level)
for (curNode = gccxml.children; curNode; curNode = curNode.next) {
if (curNode.type == xmlElementType.XML_ELEMENT_NODE) {
- char[] nname = toString(curNode.name);
-
+ string nname = to!(string)(curNode.name);
+
//writefln(" %s", nname);
-
+
if (nname == "Namespace") {
parse_Namespace(curNode);
} else if (nname == "Typedef") {
@@ -566,8 +566,8 @@ void parse_GCC_XML()
parse_Enumeration(curNode);
} else if (nname == "Class") {
// special case for template classes
- char[] sname = toStringFree(xmlGetProp(curNode, "name"));
- if (find(sname, "<DReflectedClass>") != -1) {
+ string sname = toStringFree(xmlGetProp(curNode, "name".dup.ptr));
+ if (sname == "<DReflectedClass>") {
parse_Class(curNode);
}
}
@@ -578,26 +578,26 @@ void parse_GCC_XML()
/**
* Parse a GCC_XML node for a specified ID
*/
-void parse_GCC_XML_for(char[] parseFor, bool inclass, bool types, bool reflection = false)
+void parse_GCC_XML_for(string parseFor, bool inclass, bool types, bool reflection = false)
{
xmlNode *curNode = null;
int xmlrret;
-
+
for (curNode = gccxml.children; curNode; curNode = curNode.next) {
if (curNode.type == xmlElementType.XML_ELEMENT_NODE) {
- char[] nname = toString(curNode.name);
-
- char *id = xmlGetProp(curNode, "id");
+ string nname = to!(string)(curNode.name);
+
+ char *id = xmlGetProp(curNode, "id".dup.ptr);
if (parseFor != toStringFree(id)) continue;
if (nname == "Constructor") {
// this may be private or otherwise unparsable, but we do have one
hasConstructor = true;
}
-
+
// types that can be nameless:
if (!parseThis(curNode, true)) continue;
-
+
if (nname == "Struct" || nname == "Class") {
// structs and classes are the same in C[++]
if (outputC) {
@@ -610,12 +610,12 @@ void parse_GCC_XML_for(char[] parseFor,
if (types) parse_Struct(curNode);
continue;
}
-
+
// types that cannot be nameless:
if (!parseThis(curNode)) continue;
-
+
//writefln(" %s", nname);
-
+
if (nname == "Variable" || nname == "Field") {
if (!types && !reflection) parse_Variable(curNode, inclass);
} else if (nname == "Method") {
@@ -646,36 +646,36 @@ void parse_File(xmlNode *node)
{
// associate the id with the filename
char* id, name;
- id = xmlGetProp(node, "id");
- name = xmlGetProp(node, "name");
+ id = xmlGetProp(node, "id".dup.ptr);
+ name = xmlGetProp(node, "name".dup.ptr);
if (id && name) {
- char[] sname = toString(name);
-
+ string sname = to!(string)(name);
+
files[toStringFree(id)] = sname;
-
+
// import it in D
-
+
// first try our own namespace if we didn't use -A
if (!outputAll && getDirName(sname) == baseDir) {
- char[] baseName = sname[baseDir.length + 1 .. sname.length];
- if (find(baseName, '.') != -1) {
+ string baseName = sname[baseDir.length + 1 .. sname.length];
+ if (baseName != ".") {
baseName = getName(baseName);
}
-
+
if (baseName != shortName)
dhead ~= "public import " ~ dNamespaceBase ~ dNamespace ~ "." ~ safeName(baseName) ~ ";\n";
}
-
+
// then others
foreach (req; reqDependencies.keys) {
if (getDirName(sname) == req) {
- char[] baseName = sname[req.length + 1 .. sname.length];
- if (find(baseName, '.') != -1) {
+ string baseName = sname[req.length + 1 .. sname.length];
+ if (baseName != ".") {
baseName = getName(baseName);
}
-
+
baseName = safeName(baseName);
-
+
if (baseName != shortName)
dhead ~= "public import " ~ dNamespaceBase ~ reqDependencies[req] ~ "." ~ safeName(baseName) ~ ";\n";
}
@@ -700,60 +700,60 @@ void parse_Namespace(xmlNode *node)
*/
void parse_Class(xmlNode *node)
{
- char[] name = getNName(node);
- char[] mangled = toStringFree(getMangled(node));
- char[] demangled = toStringFree(getDemangled(node));
- char[] prevCurClass = curClass;
- char* isabstract = xmlGetProp(node, "abstract");
+ string name = getNName(node);
+ string mangled = toStringFree(getMangled(node));
+ string demangled = toStringFree(getDemangled(node));
+ string prevCurClass = curClass;
+ char* isabstract = xmlGetProp(node, "abstract".dup.ptr);
if (isabstract) free(isabstract);
curClass = demangled;
curClassAbstract = isabstract ? true : false;
hasConstructor = false;
hasPublicConstructor = false;
-
+
parseMembers(node, true, true);
-
+
// parse for base classes
- char[] base = "bcd.bind.BoundClass";
+ string base = "bcd.bind.BoundClass";
xmlNode *curNode = null;
for (curNode = node.children; curNode; curNode = curNode.next) {
if (curNode.type == xmlElementType.XML_ELEMENT_NODE) {
- if (toString(curNode.name) == "Base") {
- ParsedType pt = parseType(toStringFree(xmlGetProp(curNode, "type")));
+ if (to!(string)(curNode.name) == "Base") {
+ ParsedType pt = parseType(toStringFree(xmlGetProp(curNode, "type".dup.ptr)));
base = pt.DType;
break;
}
}
}
-
+
// if this is derived from a template, the derivation is worthless from D
- if (find(base, '<') != -1) base = "bcd.bind.BoundClass";
-
+ if (base != "<") base = "bcd.bind.BoundClass";
+
dtail ~= "class " ~ safeName(name) ~ " : " ~ base ~ " {\n";
-
+
dtail ~= "this(ifloat ignore) {\n";
dtail ~= "super(ignore);\n";
dtail ~= "}\n";
-
+
dtail ~= "this(ifloat ignore, void *x) {\n";
dtail ~= "super(ignore);\n";
dtail ~= "__C_data = x;\n";
dtail ~= "__C_data_owned = false;\n";
dtail ~= "}\n";
-
+
cout ~= "void _BCD_delete_" ~ mangled ~ "(" ~ demangled ~ " *This) {\n";
cout ~= "delete This;\n";
cout ~= "}\n";
-
+
dhead ~= "extern (C) void _BCD_delete_" ~ mangled ~ "(void *);\n";
-
+
dtail ~= "~this() {\n";
dtail ~= "if (__C_data && __C_data_owned) _BCD_delete_" ~ mangled ~ "(__C_data);\n";
dtail ~= "__C_data = null;\n";
dtail ~= "}\n";
-
+
parseMembers(node, true, false);
-
+
// if the constructor is implicit, replicate it here
if (!hasConstructor && !isabstract) {
dhead ~= "extern (C) void *_BCD_new_" ~ mangled ~ "();\n";
@@ -766,9 +766,9 @@ void parse_Class(xmlNode *node)
cout ~= "return new " ~ curClass ~ "();\n";
cout ~= "}\n";
}
-
+
dtail ~= "}\n";
-
+
// now make the reflected class
curClass = prevCurClass;
if (!outputReflections) return;
@@ -778,20 +778,20 @@ void parse_Class(xmlNode *node)
curReflectionDBase = safeName(name);
curReflection = curReflectionDBase ~ "_R";
curReflectionInit = "_BCD_RI_" ~ mangled;
-
+
dhead ~= "extern (C) void " ~ curReflectionInit ~ "(void *cd, void *dd);\n";
-
+
dtail ~= "class " ~ curReflection ~ " : " ~ curReflectionDBase ~ " {\n";
-
+
dhead ~= "extern (C) void _BCD_delete_" ~ mangled ~ "__" ~ curReflection ~ "(void *This);\n";
dtail ~= "~this() {\n";
dtail ~= "if (__C_data && __C_data_owned) _BCD_delete_" ~ mangled ~ "__" ~ curReflection ~ "(__C_data);\n";
dtail ~= "__C_data = null;\n";
dtail ~= "}\n";
-
+
reflectionPostCode = "";
-
+
reflectionCode = "}\n"; // close the extern "C"
reflectionCode ~= "class " ~ curReflection ~ " : " ~ curReflectionCBase ~ " {\n";
reflectionCode ~= "public:\n";
@@ -800,19 +800,19 @@ void parse_Class(xmlNode *node)
hasConstructor = false;
hasPublicConstructor = false;
parseBaseReflections(node);
-
+
reflectionCode ~= "};\n";
reflectionCode ~= "extern \"C\" {\n";
-
+
cout ~= reflectionCode ~ reflectionPostCode;
-
+
reflectionCode = "";
reflectedFunctions = null;
-
+
cout ~= "void _BCD_delete_" ~ mangled ~ "__" ~ curReflection ~ "(" ~ curReflection ~ " *This) {\n";
cout ~= "delete This;\n";
cout ~= "}\n";
-
+
// if the constructor is implicit, make it here
if (!hasConstructor) {
dhead ~= "extern (C) void *_BCD_new_" ~ mangled ~ "__" ~ curReflection ~ "();\n";
@@ -827,9 +827,9 @@ void parse_Class(xmlNode *node)
} else if (!hasPublicConstructor) {
dtail ~= "this() { super(cast(ireal) 0); }\n";
}
-
+
dtail ~= "}\n";
-
+
// then make the initializer
cout ~= "void _BCD_RI_" ~ mangled ~ "(" ~ curReflection ~ " *cd, void *dd) {\n";
cout ~= "cd->__D_data = dd;\n";
@@ -846,24 +846,24 @@ void parseBaseReflections(xmlNode *node)
xmlNode *curNode = null;
for (curNode = node.children; curNode; curNode = curNode.next) {
if (curNode.type == xmlElementType.XML_ELEMENT_NODE) {
- if (toString(curNode.name) == "Base") {
-
+ if (to!(string)(curNode.name) == "Base") {
+
// find the base class
- char[] type = toStringFree(xmlGetProp(curNode, "type"));
+ string type = toStringFree(xmlGetProp(curNode, "type".dup.ptr));
xmlNode *curBCNode = null;
for (curBCNode = gccxml.children; curBCNode; curBCNode = curBCNode.next) {
if (curBCNode.type == xmlElementType.XML_ELEMENT_NODE) {
- if (type == toStringFree(xmlGetProp(curBCNode, "id"))) {
+ if (type == toStringFree(xmlGetProp(curBCNode, "id".dup.ptr))) {
// parse this one too
parseBaseReflections(curBCNode);
}
}
}
-
+
}
}
}
-
+
// then parse this level
parseMembers(node, true, false, true);
}
@@ -873,23 +873,23 @@ void parseBaseReflections(xmlNode *node)
*/
void parse_Struct(xmlNode *node)
{
- char[] type = toString(node.name);
- char[] name = getNName(node);
- char[] mangled = toStringFree(getMangled(node));
- char[] demangled = toStringFree(getDemangled(node));
- char[] prevCurClass = curClass;
-
+ string type = to!(string)(node.name);
+ string name = getNName(node);
+ string mangled = toStringFree(getMangled(node));
+ string demangled = toStringFree(getDemangled(node));
+ string prevCurClass = curClass;
+
parseMembers(node, true, true);
-
+
if (type == "Union") {
dtail ~= "union ";
} else {
dtail ~= "struct ";
}
dtail ~= safeName(name) ~ " {\n";
-
+
curClass = demangled;
-
+
parseMembers(node, true, false);
dtail ~= "}\n";
@@ -901,11 +901,11 @@ void parse_Struct(xmlNode *node)
*/
void parse_Variable(xmlNode *node, bool inclass)
{
- char[] stype = toStringFree(xmlGetProp(node, "type"));
+ string stype = toStringFree(xmlGetProp(node, "type".dup.ptr));
ParsedType type;
- char[] name = getNName(node);
- char[] mangled = toStringFree(getMangled(node));
-
+ string name = getNName(node);
+ string mangled = toStringFree(getMangled(node));
+
if (outputC) {
type = parseType(stype);
if (!inclass) {
@@ -918,11 +918,11 @@ void parse_Variable(xmlNode *node, bool
// if it's a const, don't make the set
if (stype[stype.length - 1] != 'c') {
dhead ~= "extern (C) void _BCD_set_" ~ mangled ~ "(void *, " ~ type.DType ~ ");\n";
-
+
dtail ~= "void set_" ~ safeName(name) ~ "(" ~ type.DType ~ " x) {\n";
dtail ~= "_BCD_set_" ~ mangled ~ "(__C_data, x);\n";
dtail ~= "}\n";
-
+
cout ~= "void _BCD_set_" ~ mangled ~ "(" ~ curClass ~ " *This, " ~ type.CType ~ " x) {\n";
if (!type.isClass) {
cout ~= "This->" ~ name ~ " = x;\n";
@@ -931,29 +931,29 @@ void parse_Variable(xmlNode *node, bool
}
cout ~= "}\n";
}
-
+
dhead ~= "extern (C) " ~ type.DType ~ " _BCD_get_" ~ mangled ~ "(void *);\n";
-
+
dtail ~= type.DType ~ " get_" ~ safeName(name) ~ "() {\n";
dtail ~= "return _BCD_get_" ~ mangled ~ "(__C_data);\n";
dtail ~= "}\n";
-
+
cout ~= type.CType ~ " _BCD_get_" ~ mangled ~ "(" ~ curClass ~ " *This) {\n";
cout ~= "return ";
if (type.isClass) cout ~= "&";
cout ~= "This->" ~ name ~ ";\n";
cout ~= "}\n";
} else {
- char[] demangled = toStringFree(getDemangled(node));
-
+ string demangled = toStringFree(getDemangled(node));
+
// if it's a const, don't make the set
if (stype[stype.length - 1] != 'c') {
dhead ~= "extern (C) void _BCD_set_" ~ mangled ~ "(" ~ type.DType ~ ");\n";
-
+
dtail ~= "void set_" ~ safeName(name) ~ "(" ~ type.DType ~ " x) {\n";
dtail ~= "_BCD_set_" ~ mangled ~ "(x);\n";
dtail ~= "}\n";
-
+
cout ~= "void _BCD_set_" ~ mangled ~ "(" ~ type.CType ~ " x) {\n";
if (!type.isClass) {
cout ~= demangled ~ " = x;\n";
@@ -962,13 +962,13 @@ void parse_Variable(xmlNode *node, bool
}
cout ~= "}\n";
}
-
+
dhead ~= "extern (C) " ~ type.DType ~ " _BCD_get_" ~ mangled ~ "();\n";
-
+
dtail ~= type.DType ~ " get_" ~ safeName(name) ~ "() {\n";
dtail ~= "return _BCD_get_" ~ mangled ~ "();\n";
dtail ~= "}\n";
-
+
cout ~= type.CType ~ " _BCD_get_" ~ mangled ~ "() {\n";
cout ~= "return ";
if (type.isClass) cout ~= "&";
@@ -981,37 +981,35 @@ void parse_Variable(xmlNode *node, bool
/**
* Parse Argument nodes
*/
-void parse_Arguments(xmlNode *node, inout char[] Dargs, inout char[] Deargs,
- inout char[] Cargs, inout char[] Dcall,
- inout char[] Ccall, bool reflection = false,
+void parse_Arguments(xmlNode *node, ref string Dargs, ref string Deargs,
+ ref string Cargs, ref string Dcall,
+ ref string Ccall, bool reflection = false,
int *argc = null)
{
int onParam = 0;
xmlNode *curNode = null;
-
+
for (curNode = node.children; curNode; curNode = curNode.next) {
if (curNode.type == xmlElementType.XML_ELEMENT_NODE) {
- char[] nname = toString(curNode.name);
- char[] def = toString(xmlGetProp(curNode, "default"));
+ string nname = to!(string)(curNode.name);
+ string def = to!(string)(xmlGetProp(curNode, "default".dup.ptr));
if(def == "NULL")
def = "null";
if (nname == "Argument") {
- ParsedType atype = parseType(toStringFree(xmlGetProp(curNode, "type")));
- char[] aname = getNName(curNode);
- if (aname == "") aname = "_" ~ toString(onParam);
+ ParsedType atype = parseType(toStringFree(xmlGetProp(curNode, "type".dup.ptr)));
+ string aname = getNName(curNode);
+ if (aname == "") aname = "_" ~ to!(string)(onParam);
aname = safeName(aname);
-
- if(def == "0" &&
- (find(atype.DType, "*") != -1 ||
- atype.isFunctionPtr))
+
+ if(def == "0" && (atype.DType == "*") || atype.isFunctionPtr)
def = "null";
if (Dargs != "") {
Dargs ~= ", ";
}
-
+
if (!reflection || (!atype.isClass && !atype.isClassPtr)) {
if(def != "" && defaultValues)
Dargs ~= atype.DType ~ " " ~ aname ~ " = " ~ def;
@@ -1020,7 +1018,7 @@ void parse_Arguments(xmlNode *node, inou
} else {
Dargs ~= "void *" ~ aname;
}
-
+
if (!reflection && (atype.isClass || atype.isClassPtr)) {
// this becomes a void * in D's view
if (Deargs != "") {
@@ -1033,12 +1031,12 @@ void parse_Arguments(xmlNode *node, inou
}
Deargs ~= atype.DType;
}
-
+
if (Cargs != "") {
Cargs ~= ", ";
}
Cargs ~= atype.CType ~ " " ~ aname;
-
+
if (Dcall != "") {
Dcall ~= ", ";
}
@@ -1052,12 +1050,12 @@ void parse_Arguments(xmlNode *node, inou
if (atype.isClass) {
Dcall ~= "new " ~ atype.className ~ "(cast(ifloat) 0, " ~ aname ~ ")";
} else if (atype.isClassPtr) {
- Dcall ~= "cast(" ~ atype.DType ~ ") new " ~ replace(atype.DType, " *", "") ~ "(cast(ifloat) 0, " ~ aname ~ ")";
+ Dcall ~= "cast(" ~ atype.DType ~ ") new " ~ tr(atype.DType, " *", "") ~ "(cast(ifloat) 0, " ~ aname ~ ")";
} else {
Dcall ~= aname;
}
}
-
+
if (atype.isClass) {
// need to dereference
if (Ccall != "") {
@@ -1074,51 +1072,51 @@ void parse_Arguments(xmlNode *node, inou
}
Ccall ~= aname;
}
-
+
if (argc) (*argc)++;
-
+
} else if (outputC && nname == "Ellipsis") {
if (Dargs != "") {
Dargs ~= ", ";
}
Dargs ~= "...";
-
+
if (Deargs != "") {
Deargs ~= ", ";
}
Deargs ~= "...";
-
+
if (Cargs != "") {
Cargs ~= ", ";
}
Cargs ~= "...";
-
+
if (Dcall != "") {
Dcall ~= ", ";
}
Dcall ~= "...";
-
+
if (Ccall != "") {
Ccall ~= ", ";
}
Ccall ~= "...";
-
+
} else {
writefln("I don't know how to parse %s!", nname);
}
-
+
onParam++;
}
}
}
-void parse_Function_body(xmlNode *node, char[] name, char[] mangled, char[] demangled, ParsedType type,
- char[] Dargs, char[] Deargs, char[] Cargs, char[] Dcall, char[] Ccall,
+void parse_Function_body(xmlNode *node, string name, string mangled, string demangled, ParsedType type,
+ string Dargs, string Deargs, string Cargs, string Dcall, string Ccall,
bool isStatic = false)
{
// make sure it's not already defined (particularly problematic for overrides that aren't overrides in D)
- static bool[char[]] handledFunctions;
- char[] fid = curClass ~ "::" ~ demangled ~ "(" ~ Deargs ~ ")";
+ static bool[string] handledFunctions;
+ string fid = curClass ~ "::" ~ demangled ~ "(" ~ Deargs ~ ")";
if (fid in handledFunctions) return;
handledFunctions[fid] = true;
@@ -1126,9 +1124,9 @@ void parse_Function_body(xmlNode *node,
dhead ~= "extern (C) " ~ type.DType ~ " " ~ demangled ~ "(" ~ Deargs ~ ");\n";
return;
}
-
+
dhead ~= "extern (C) " ~ type.DType ~ " _BCD_" ~ mangled ~ "(" ~ Deargs ~ ");\n";
-
+
if (!type.isClass) {
dtail ~= (isStatic ? "static " : "") ~
type.DType ~ " " ~ name ~ "(" ~ Dargs ~ ") {\n";
@@ -1137,7 +1135,7 @@ void parse_Function_body(xmlNode *node,
}
dtail ~= "_BCD_" ~ mangled ~ "(" ~ Dcall ~ ");\n";
dtail ~= "}\n";
-
+
cout ~= type.CType ~ " _BCD_" ~ mangled ~ "(" ~ Cargs ~ ") {\n";
if (type.DType != "void") {
cout ~= "return ";
@@ -1152,52 +1150,52 @@ void parse_Function_body(xmlNode *node,
dtail ~= "dret.__C_data = cret;\n";
dtail ~= "return dret;\n";
dtail ~= "}\n";
-
+
cout ~= type.CType ~ " _BCD_" ~ mangled ~ "(" ~ Cargs ~ ") {\n";
cout ~= "return new " ~ type.className ~ "(" ~ demangled ~ "(" ~ Ccall ~ "));\n";
cout ~= "}\n";
}
}
-void parse_Function_reflection(xmlNode *node, char[] name, char[] cname,
- char[] mangled, ParsedType type,
- char[] Dargs, char[] Deargs, char[] Cargs, char[] Dcall, char[] Ccall)
+void parse_Function_reflection(xmlNode *node, string name, string cname,
+ string mangled, ParsedType type,
+ string Dargs, string Deargs, string Cargs, string Dcall, string Ccall)
{
// tie to the particular class being reflected
mangled ~= "__" ~ curReflection;
// make sure it's not already reflected
- char[] fid = name ~ "(" ~ Deargs ~ ")";
+ string fid = name ~ "(" ~ Deargs ~ ")";
if (fid in reflectedFunctions) return;
reflectedFunctions[fid] = true;
-
+
// make sure it's virtual
- char* isvirtual = xmlGetProp(node, "virtual");
+ char* isvirtual = xmlGetProp(node, "virtual".dup.ptr);
if (isvirtual) free(isvirtual);
else return;
-
+
// the C++ interface to the reflection
cout ~= "int _BCD_R_" ~ mangled ~ "_CHECK(void *);\n";
-
+
if (Cargs != "")
cout ~= type.CType ~ " _BCD_R_" ~ mangled ~ "(void *, " ~ Cargs ~ ");\n";
else
cout ~= type.CType ~ " _BCD_R_" ~ mangled ~ "(void *);\n";
-
+
reflectionCode ~= type.CType ~ " " ~ name ~ "(" ~ Cargs ~ ") {\n";
-
+
reflectionCode ~= "if (_BCD_R_" ~ mangled ~ "_CHECK(__D_data))\n";
if (type.CType != "void") reflectionCode ~= "return ";
-
+
reflectionCode ~= "_BCD_R_" ~ mangled ~ "(__D_data";
if (Ccall != "") reflectionCode ~= ", ";
reflectionCode ~= Ccall ~ ");\n";
-
+
reflectionCode ~= "else\n";
if (type.CType != "void") reflectionCode ~= "return ";
reflectionCode ~= curReflectionCBase ~ "::" ~ cname ~ "(" ~ Ccall ~ ");\n";
reflectionCode ~= "}\n";
-
+
// and the D interface
dhead ~= "extern (C) int _BCD_R_" ~ mangled ~ "_CHECK(" ~ curReflection ~ " x) {\n";
dhead ~= "union dp {\n";
@@ -1207,7 +1205,7 @@ void parse_Function_reflection(xmlNode *
dhead ~= "dp d; d.d = &x." ~ name ~ ";\n";
dhead ~= "return cast(int) (d.f != &" ~ curReflectionDBase ~ "." ~ name ~ ");\n";
dhead ~= "}\n";
-
+
dhead ~= "extern (C) " ~ type.DType ~ " _BCD_R_" ~ mangled ~ "(" ~ curReflection ~ " __D_class, " ~
Dargs ~ ") {\n";
if (type.DType != "void") dhead ~= "return ";
@@ -1221,24 +1219,24 @@ void parse_Function_reflection(xmlNode *
void parse_Method(xmlNode *node, bool reflection)
{
/* If it's static, it's for all intents and purposes a function */
- if (toStringFree(xmlGetProp(node, "static")) == "1") {
+ if (toStringFree(xmlGetProp(node, "static".dup.ptr)) == "1") {
if (!reflection)
parse_Function(node, true);
return;
}
-
- char[] name = getNName(node);
- char[] mangled = toStringFree(getMangled(node));
- ParsedType type = parseTypeReturnable(toStringFree(xmlGetProp(node, "returns")));
- char[] Dargs;
- char[] Deargs;
+
+ string name = getNName(node);
+ string mangled = toStringFree(getMangled(node));
+ ParsedType type = parseTypeReturnable(toStringFree(xmlGetProp(node, "returns".dup.ptr)));
+ string Dargs;
+ string Deargs;
if (!reflection) Deargs = "void *This";
- char[] Cargs;
+ string Cargs;
if (!reflection) Cargs = curClass ~ " *This";
- char[] Dcall;
+ string Dcall;
if (!reflection) Dcall = "__C_data";
- char[] Ccall;
-
+ string Ccall;
+
parse_Arguments(node, Dargs, Deargs, Cargs, Dcall, Ccall, reflection);
if (!reflection)
parse_Function_body(node, safeName(name), mangled, "This->" ~ name, type,
@@ -1253,23 +1251,23 @@ void parse_Method(xmlNode *node, bool re
*/
void parse_OperatorMethod(xmlNode *node, bool reflection)
{
- char[] name = toStringFree(xmlGetProp(node, "name"));;
- char[] mangled = toStringFree(getMangled(node));
- ParsedType type = parseTypeReturnable(toStringFree(xmlGetProp(node, "returns")));
- char[] Dargs;
- char[] Deargs;
+ string name = toStringFree(xmlGetProp(node, "name".dup.ptr));
+ string mangled = toStringFree(getMangled(node));
+ ParsedType type = parseTypeReturnable(toStringFree(xmlGetProp(node, "returns".dup.ptr)));
+ string Dargs;
+ string Deargs;
if (!reflection) Deargs = "void *This";
- char[] Cargs;
+ string Cargs;
if (!reflection) Cargs = curClass ~ " *This";
- char[] Dcall;
+ string Dcall;
if (!reflection) Dcall = "__C_data";
- char[] Ccall;
+ string Ccall;
int argc;
-
+
parse_Arguments(node, Dargs, Deargs, Cargs, Dcall, Ccall, reflection, &argc);
-
+
// get the D name
- char[] dname;
+ string dname;
switch (name) {
case "-":
if (argc == 0) {
@@ -1278,7 +1276,7 @@ void parse_OperatorMethod(xmlNode *node,
dname = "opSub";
}
break;
-
+
case "+":
if (argc == 0) {
dname = "opPos";
@@ -1286,7 +1284,7 @@ void parse_OperatorMethod(xmlNode *node,
dname = "opAdd";
}
break;
-
+
case "++":
if (argc == 0) {
dname = "opPostInc";
@@ -1295,7 +1293,7 @@ void parse_OperatorMethod(xmlNode *node,
dname = "opPreInc";
}
break;
-
+
case "--":
if (argc == 0) {
dname = "opPostDec";
@@ -1304,113 +1302,113 @@ void parse_OperatorMethod(xmlNode *node,
dname = "opPreDec";
}
break;
-
+
case "*":
dname = "opMul";
break;
-
+
case "/":
dname = "opDiv";
break;
-
+
case "%":
dname = "opMod";
break;
-
+
case "&":
dname = "opAnd";
break;
-
+
case "|":
dname = "opOr";
break;
-
+
case "^":
dname = "opXor";
break;
-
+
case "<<":
dname = "opShl";
break;
-
+
case ">>":
dname = "opShr";
break;
-
+
case "==":
dname = "opEquals";
break;
-
+
case "!=":
// not a real operator, but accessable
dname = "opNotEquals";
break;
-
+
case "<":
// not a real operator, but accessable
dname = "opLT";
break;
-
+
case "<=":
// not a real operator, but accessable
dname = "opLE";
break;
-
+
case ">":
// not a real operator, but accessable
dname = "opGT";
break;
-
+
case ">=":
// not a real operator, but accessable
dname = "opGE";
break;
-
+
case "+=":
dname = "opAddAssign";
break;
-
+
case "-=":
dname = "opSubAssign";
break;
-
+
case "*=":
dname = "opMulAssign";
break;
-
+
case "/=":
dname = "opDivAssign";
break;
-
+
case "%=":
dname = "opModAssign";
break;
-
+
case "&=":
dname = "opAndAssign";
break;
-
+
case "|=":
dname = "opOrAssign";
break;
-
+
case "^=":
dname = "opXorAssign";
break;
-
+
case "<<=":
dname = "opShlAssign";
break;
-
+
case ">>=":
dname = "opShrAssign";
break;
-
+
default:
}
-
+
if (dname == "") return;
-
+
if (!reflection)
parse_Function_body(node, dname, mangled, "This->operator" ~ name, type,
Dargs, Deargs, Cargs, Dcall, Ccall);
@@ -1424,22 +1422,22 @@ void parse_OperatorMethod(xmlNode *node,
*/
void parse_Function(xmlNode *node, bool isStatic = false)
{
- char[] name = getNName(node);
- char[] mangled = toStringFree(getMangled(node));
- char[] demangled = toStringFree(getDemangled(node));
- ParsedType type = parseTypeReturnable(toStringFree(xmlGetProp(node, "returns")));
- char[] Dargs;
- char[] Deargs;
- char[] Cargs;
- char[] Dcall;
- char[] Ccall;
-
+ string name = getNName(node);
+ string mangled = toStringFree(getMangled(node));
+ string demangled = toStringFree(getDemangled(node));
+ ParsedType type = parseTypeReturnable(toStringFree(xmlGetProp(node, "returns".dup.ptr)));
+ string Dargs;
+ string Deargs;
+ string Cargs;
+ string Dcall;
+ string Ccall;
+
// the demangled name includes ()
- int demparen = find(demangled, '(');
+ int demparen = (demangled == "(") ? 0 : -1;
if (demparen != -1) {
demangled = demangled[0..demparen];
}
-
+
parse_Arguments(node, Dargs, Deargs, Cargs, Dcall, Ccall);
parse_Function_body(node, safeName(name), mangled, demangled, type,
Dargs, Deargs, Cargs, Dcall, Ccall, isStatic);
@@ -1453,49 +1451,49 @@ void parse_Constructor(xmlNode *node, bo
if (outputC) return; // no constructors in C
if (curClassAbstract) return; // no constructors for virtual classes
- char[] name = getNName(node);
- char[] mangled = toStringFree(getMangled(node));
+ string name = getNName(node);
+ string mangled = toStringFree(getMangled(node));
if (reflection) mangled ~= "_R";
-
- while (find(mangled, "*INTERNAL*") != -1) {
- mangled = replace(mangled, " *INTERNAL* ", "");
+
+ while (mangled != "*INTERNAL*") {
+ mangled = tr(mangled, " *INTERNAL* ", "");
}
// no artificial constructors
- char* artificial = xmlGetProp(node, "artificial");
+ char* artificial = xmlGetProp(node, "artificial".dup.ptr);
if (artificial) {
free(artificial);
return;
}
-
- char[] Dargs;
- char[] Deargs;
- char[] Cargs;
- char[] Dcall;
- char[] Ccall;
-
+
+ string Dargs;
+ string Deargs;
+ string Cargs;
+ string Dcall;
+ string Ccall;
+
if (reflection) {
// only reflect one level of constructors
if (name != curReflectionDBase) return;
}
-
+
parse_Arguments(node, Dargs, Deargs, Cargs, Dcall, Ccall, false);
-
+
// make sure it's not already defined (particularly problematic for overrides that aren't overrides in D)
if (!reflection) {
- static bool[char[]] handledCtors;
- char[] fid = curClass ~ "(" ~ Deargs ~ ")";
+ static bool[string] handledCtors;
+ string fid = curClass ~ "(" ~ Deargs ~ ")";
if (fid in handledCtors) return;
handledCtors[fid] = true;
} else if (reflection) {
// make sure it's not already reflected
- char[] sfid = name ~ "(" ~ Deargs ~ ")";
+ string sfid = name ~ "(" ~ Deargs ~ ")";
if (sfid in reflectedFunctions) return;
reflectedFunctions[sfid] = true;
}
-
+
dhead ~= "extern (C) void *_BCD_new_" ~ mangled ~ "(" ~ Deargs ~ ");\n";
-
+
dtail ~= "this(" ~ Dargs ~ ") {\n";
dtail ~= "super(cast(ifloat) 0);\n";
dtail ~= "__C_data = _BCD_new_" ~ mangled ~ "(" ~ Dcall ~ ");\n";
@@ -1504,7 +1502,7 @@ void parse_Constructor(xmlNode *node, bo
dtail ~= curReflectionInit ~ "(__C_data, cast(void *) this);\n";
}
dtail ~= "}\n";
-
+
if (!reflection) {
cout ~= curClass ~ " *_BCD_new_" ~ mangled ~ "(" ~ Cargs ~ ") {\n";
cout ~= "return new ";
@@ -1528,19 +1526,19 @@ void parse_Constructor(xmlNode *node, bo
*/
void parse_Typedef(xmlNode *node)
{
- static bool[char[]] handledTypedefs;
-
- char[] type = toStringFree(xmlGetProp(node, "id"));
- char[] deftype = toStringFree(xmlGetProp(node, "type"));
-
+ static bool[string] handledTypedefs;
+
+ string type = toStringFree(xmlGetProp(node, "id".dup.ptr));
+ string deftype = toStringFree(xmlGetProp(node, "type".dup.ptr));
+
ParsedType pt = parseType(deftype);
- char[] aname = getNName(node);
-
+ string aname = getNName(node);
+
if (!(type in handledTypedefs)) {
handledTypedefs[type] = true;
-
+
cout ~= "typedef " ~ pt.CType ~ " _BCD_" ~ type ~ "_" ~ aname ~ ";\n";
-
+
if (parseThis(node, true)) dhead ~= "alias " ~ pt.DType ~ " " ~ safeName(aname) ~ ";\n";
}
}
@@ -1550,75 +1548,75 @@ void parse_Typedef(xmlNode *node)
*/
void parse_Enumeration(xmlNode *node)
{
- static bool[char[]] handledEnums;
-
+ static bool[string] handledEnums;
+
if (!parseThis(node, true)) return;
-
- char[] aname = getNName(node);
+
+ string aname = getNName(node);
if (aname == "") return;
-
- char* realName = xmlGetProp(node, "name");
+
+ char* realName = xmlGetProp(node, "name".dup.ptr);
if (realName && realName[0] == '.') {
// this is artificial, no real name
free(realName);
realName = null;
}
if (realName) free(realName);
-
- char[] type = toStringFree(xmlGetProp(node, "id"));
-
+
+ string type = toStringFree(xmlGetProp(node, "id".dup.ptr));
+
// make an enum in D as well
if (!(type in handledEnums)) {
handledEnums[type] = true;
-
+
xmlNode *curNode = null;
-
+
if (aname[0] != '.')
{
-
+
dhead ~= "enum " ~ safeName(aname) ~ " {\n";
-
-
+
+
for (curNode = node.children; curNode; curNode = curNode.next) {
if (curNode.type == xmlElementType.XML_ELEMENT_NODE) {
- char[] nname = toString(curNode.name);
-
+ string nname = to!(string)(curNode.name);
+
if (nname == "EnumValue") {
dhead ~= safeName(getNName(curNode)) ~ "=" ~
- toStringFree(xmlGetProp(curNode, "init")) ~ ",\n";
+ toStringFree(xmlGetProp(curNode, "init".dup.ptr)) ~ ",\n";
} else {
writefln("I don't know how to parse %s!", nname);
}
}
}
-
+
dhead ~= "}\n";
if(polluteNamespace)
{
- for (curNode = node.children; curNode; curNode = curNode.next) {
- if (curNode.type == xmlElementType.XML_ELEMENT_NODE) {
- char[] nname = toString(curNode.name);
-
- if (nname == "EnumValue") {
+ for (curNode = node.children; curNode; curNode = curNode.next) {
+ if (curNode.type == xmlElementType.XML_ELEMENT_NODE) {
+ string nname = to!(string)(curNode.name);
+
+ if (nname == "EnumValue") {
dhead ~= "alias " ~ safeName(aname) ~ "." ~ safeName(getNName(curNode)) ~ " " ~
safeName(getNName(curNode)) ~ ";\n";
- } else {
- writefln("I don't know how to parse %s!", nname);
- }
- }
- }
+ } else {
+ writefln("I don't know how to parse %s!", nname);
+ }
+ }
+ }
}
}
// then generate consts for it
if (outputEnumConst && !realName) {
for (curNode = node.children; curNode; curNode = curNode.next) {
if (curNode.type == xmlElementType.XML_ELEMENT_NODE) {
- char[] nname = toString(curNode.name);
-
+ string nname = to!(string)(curNode.name);
+
if (nname == "EnumValue") {
dhead ~= "const int " ~ safeName(getNName(curNode)) ~ " = " ~
- toStringFree(xmlGetProp(curNode, "init")) ~ ";\n";
+ toStringFree(xmlGetProp(curNode, "init".dup.ptr)) ~ ";\n";
} else {
writefln("I don't know how to parse %s!", nname);
}
@@ -1632,18 +1630,18 @@ void parse_Enumeration(xmlNode *node)
* A type in both C[++] and D
*/
class ParsedType {
- this(char[] sCType, char[] sDType)
+ this(string sCType, string sDType)
{
CType ~= sCType;
DType ~= sDType;
}
-
+
this(ParsedType copy)
{
CType ~= copy.CType;
DType ~= copy.DType;
}
-
+
ParsedType dup()
{
ParsedType pt = new ParsedType(this);
@@ -1655,10 +1653,10 @@ class ParsedType {
pt.isStaticArray = isStaticArray;
return pt;
}
-
- char[] CType;
- char[] DType;
- char[] className;
+
+ string CType;
+ string DType;
+ string className;
bool isClass;
bool isClassPtr;
bool isFunction;
@@ -1669,12 +1667,12 @@ class ParsedType {
/**
* Get the type of a node in C[++] and D, in a way which can be a D return type
*/
-ParsedType parseTypeReturnable(char[] type)
+ParsedType parseTypeReturnable(string type)
{
ParsedType t = parseType(type);
if (t.isStaticArray) {
// can't return a static array, convert it into a pointer
- int bloc = rfind(t.DType, '[');
+ sizediff_t bloc = lastIndexOf(t.DType, '[');
if (bloc != -1) {
// cut off the [...]
t.DType = t.DType[0..bloc] ~ "*";
@@ -1692,115 +1690,115 @@ version (Windows) {} else {
/**
* Get the type of a node in C[++] and D
*/
-ParsedType parseType(char[] type)
+ParsedType parseType(string type)
{
- static ParsedType[char[]] parsedCache;
-
+ static ParsedType[string] parsedCache;
+
int xmlrret;
-
+
version (Windows) {} else {
if (type == "") kill(getpid(), 11);
}
-
+
// first find the element matching the type
if (!(type in parsedCache)) {
xmlNode *curNode = null;
-
+
for (curNode = gccxml.children; curNode; curNode = curNode.next) {
if (curNode.type == xmlElementType.XML_ELEMENT_NODE) {
- char[] nname = toString(curNode.name);
-
- char[] id = toStringFree(xmlGetProp(curNode, "id"));
+ string nname = to!(string)(curNode.name);
+
+ string id = toStringFree(xmlGetProp(curNode, "id".dup.ptr));
if (id != type) continue;
-
+
if (nname == "FundamentalType") {
- char[] ctype = getNName(curNode);
-
+ string ctype = getNName(curNode);
+
switch (ctype) {
case "void":
parsedCache[type] = new ParsedType("void", "void");
break;
-
+
case "long long int":
parsedCache[type] = new ParsedType("long long int", "long");
break;
-
+
case "long long unsigned int":
parsedCache[type] = new ParsedType("long long unsigned int", "ulong");
break;
-
-
+
+
case "long int":
parsedCache[type] = new ParsedType("long int", "int");
break;
-
+
case "long unsigned int":
parsedCache[type] = new ParsedType("long unsigned int", "uint");
break;
-
-
+
+
case "int":
parsedCache[type] = new ParsedType("int", "int");
break;
-
+
case "unsigned int":
parsedCache[type] = new ParsedType("unsigned int", "uint");
break;
-
-
+
+
case "short int":
parsedCache[type] = new ParsedType("short int", "short");
break;
-
+
case "short unsigned int":
parsedCache[type] = new ParsedType("short unsigned int", "ushort");
break;
-
-
+
+
case "char":
parsedCache[type] = new ParsedType("char", "char");
break;
-
+
case "signed char":
parsedCache[type] = new ParsedType("signed char", "char");
break;
-
+
case "unsigned char":
parsedCache[type] = new ParsedType("unsigned char", "char");
break;
-
+
case "wchar_t":
parsedCache[type] = new ParsedType("wchar_t", "wchar");
break;
-
+
case "bool":
parsedCache[type] = new ParsedType("bool", "bool");
break;
-
-
+
+
case "long double":
parsedCache[type] = new ParsedType("long double", "real");
break;
-
-
+
+
case "double":
parsedCache[type] = new ParsedType("double", "double");
break;
-
-
+
+
case "float":
parsedCache[type] = new ParsedType("float", "float");
break;
-
+
default:
parsedCache[type] = new ParsedType("void", "void");
writefln("I don't know how translate %s to D.", ctype);
}
-
+
} else if (nname == "PointerType") {
ParsedType baseType =
- parseType(toStringFree(xmlGetProp(curNode, "type")));
-
+ parseType(toStringFree(xmlGetProp(curNode, "type".dup.ptr)));
+
// functions and classes are already pointers
if (!baseType.isClass && !baseType.isFunction) {
baseType.CType ~= " *";
@@ -1810,46 +1808,46 @@ ParsedType parseType(char[] type)
ParsedType pt = new ParsedType(baseType);
pt.DType ~= " *";
pt.isClassPtr = true;
-
+
// if this is a const, our const will be on the wrong side!
if (pt.CType.length >= 7 &&
pt.CType[pt.CType.length - 7 .. pt.CType.length] == "* const") {
- pt.CType[pt.CType.length - 7 .. pt.CType.length] = "const *";
+ pt.CType = pt.CType[ 0 .. pt.CType.length - 7] ~ "const *";
}
-
+
parsedCache[type] = pt;
} else if (baseType.isFunction) {
ParsedType pt = new ParsedType(baseType);
pt.isFunctionPtr = true;
parsedCache[type] = pt;
}
-
+
} else if (nname == "ArrayType") {
ParsedType baseType =
- parseType(toStringFree(xmlGetProp(curNode, "type")));
- int size = atoi(toStringFree(xmlGetProp(curNode, "max"))) + 1;
-
+ parseType(toStringFree(xmlGetProp(curNode, "type".dup.ptr)));
+ int size = atoi(toStringFree(xmlGetProp(curNode, "max".dup.ptr)).dup.ptr) + 1;
+
// make a typedef and an alias
- static bool[char[]] handledArrays;
-
+ static bool[string] handledArrays;
+
if (!(type in handledArrays)) {
handledArrays[type] = true;
-
+
cout ~= "typedef " ~ baseType.CType ~ " _BCD_array_" ~ type ~
- "[" ~ toString(size) ~ "];\n";
+ "[" ~ to!(string)(size) ~ "];\n";
}
-
+
baseType.CType = "_BCD_array_" ~ type;
- baseType.DType ~= " [" ~ toString(size) ~ "]";
-
+ baseType.DType ~= " [" ~ to!(string)(size) ~ "]";
+
ParsedType pt = new ParsedType(baseType);
pt.isStaticArray = true;
parsedCache[type] = pt;
-
+
} else if (nname == "ReferenceType") {
ParsedType baseType =
- parseType(toStringFree(xmlGetProp(curNode, "type")));
-
+ parseType(toStringFree(xmlGetProp(curNode, "type".dup.ptr)));
+
if (!baseType.isClass) {
if (outputC) {
baseType.CType ~= " *";
@@ -1857,35 +1855,35 @@ ParsedType parseType(char[] type)
baseType.CType ~= " &";
}
baseType.DType ~= " *";
-
+
parsedCache[type] = new ParsedType(baseType);
} else {
// we need to treat this as a pointer in D, but a reference in C
-
+
// 1) cut off the *
- int l = rfind(baseType.CType, '*');
- if (l != -1) baseType.CType[l] = ' ';
-
+ sizediff_t l = lastIndexOf(baseType.CType, '*');
+ if (l != -1) baseType.CType = baseType.CType[0..l] ~ ' ' ~ baseType.CType[l+1 .. $];
+
// 2) add the &
if (outputC) {
baseType.CType ~= " *";
} else {
baseType.CType ~= " &";
}
-
+
ParsedType pt = new ParsedType(baseType);
pt.isClassPtr = true;
parsedCache[type] = pt;
}
-
+
} else if (nname == "Struct" || nname == "Class") {
- char[] className = toStringFree(getDemangled(curNode));
- char[] snname = safeName(getNName(curNode));
-
+ string className = toStringFree(getDemangled(curNode));
+ string snname = safeName(getNName(curNode));
+
if (outputC) {
- char* incomplete = xmlGetProp(curNode, "incomplete");
+ char* incomplete = xmlGetProp(curNode, "incomplete".dup.ptr);
if (incomplete) free(incomplete);
-
+
if (incomplete) {
parsedCache[type] = new ParsedType("struct " ~ className,
"void");
@@ -1895,18 +1893,18 @@ ParsedType parseType(char[] type)
}
} else {
ParsedType pt;
-
+
// special case for DReflectedClass
if (className == "DReflectedClass") {
parsedCache[type] = new ParsedType("DReflectedClass",
"CXXReflectedClass");
break;
}
-
+
// can't have incomplete types in D, so call it a BoundClass in D
- char* incomplete = xmlGetProp(curNode, "incomplete");
+ char* incomplete = xmlGetProp(curNode, "incomplete".dup.ptr);
if (incomplete) free(incomplete);
-
+
if (incomplete) {
pt = new ParsedType(className ~ " *",
"bcd.bind.BoundClass");
@@ -1914,19 +1912,19 @@ ParsedType parseType(char[] type)
pt = new ParsedType(className ~ " *",
safeName(getNName(curNode)));
}
-
+
pt.className = className;
pt.isClass = true;
parsedCache[type] = pt;
}
-
+
} else if (nname == "Union") {
- char[] className = toStringFree(getDemangled(curNode));
- char[] snname = safeName(getNName(curNode));
-
- char* incomplete = xmlGetProp(curNode, "incomplete");
+ string className = toStringFree(getDemangled(curNode));
+ string snname = safeName(getNName(curNode));
+
+ char* incomplete = xmlGetProp(curNode, "incomplete".dup.ptr);
if (incomplete) free(incomplete);
-
+
if (incomplete) {
parsedCache[type] = new ParsedType("union " ~ className,
"void");
@@ -1934,13 +1932,13 @@ ParsedType parseType(char[] type)
parsedCache[type] = new ParsedType("union " ~ className,
snname);
}
-
+
} else if (nname == "CvQualifiedType") {
// this is just a const
- ParsedType pt = parseType(toStringFree(xmlGetProp(curNode, "type")));
-
+ ParsedType pt = parseType(toStringFree(xmlGetProp(curNode, "type".dup.ptr)));
+
/*if (pt.CType.length >= 2) {
- char[] pfix = pt.CType[pt.CType.length - 2 .. pt.CType.length];
+ string pfix = pt.CType[pt.CType.length - 2 .. pt.CType.length];
if (pfix == " *" ||
pfix == " &") {
pt.CType = pt.CType[0 .. pt.CType.length - 2] ~
@@ -1949,65 +1947,65 @@ ParsedType parseType(char[] type)
break;
}
}*/
-
+
/*if (pt.CType.length < 6 ||
pt.CType[0..6] != "const ")
pt.CType = "const " ~ pt.CType;*/
pt.CType ~= " const";
-
+
parsedCache[type] = pt;
-
+
} else if (nname == "Typedef") {
// this is also an alias, but we should replicate it in D
- ParsedType pt = parseType(toStringFree(xmlGetProp(curNode, "type")));
- char[] aname = getNName(curNode);
-
+ ParsedType pt = parseType(toStringFree(xmlGetProp(curNode, "type".dup.ptr)));
+ string aname = getNName(curNode);
+
parse_Typedef(curNode);
-
+
ParsedType rpt = new ParsedType("_BCD_" ~ type ~ "_" ~ aname, pt.DType);
rpt.isClass = pt.isClass;
rpt.isFunction = pt.isFunction;
rpt.isStaticArray = pt.isStaticArray;
parsedCache[type] = rpt;
-
+
} else if (nname == "FunctionType" || nname == "MethodType") {
// make a typedef and an alias
- static bool[char[]] handledFunctions;
+ static bool[string] handledFunctions;
- char[] base = "";
+ string base = "";
if (nname == "MethodType") {
- base = parseType(toStringFree(xmlGetProp(curNode, "basetype"))).CType;
+ base = parseType(toStringFree(xmlGetProp(curNode, "basetype".dup.ptr))).CType;
base = base[0 .. base.length - 2] ~ "::*";
}
-
+
if (!(type in handledFunctions)) {
handledFunctions[type] = true;
-
- ParsedType pt = parseType(toStringFree(xmlGetProp(curNode, "returns")));
- char[] couta, dheada;
-
+
+ ParsedType pt = parseType(toStringFree(xmlGetProp(curNode, "returns".dup.ptr)));
+ string couta, dheada;
+
bool first = true;
couta = "typedef " ~ pt.CType ~
" (*" ~ base ~ "_BCD_func_" ~ type ~ ")(";
dheada = "alias " ~ pt.DType ~ " function(";
-
+
// now look for arguments
xmlNode *curArg;
for (curArg = curNode.children; curArg; curArg = curArg.next) {
if (curArg.type == xmlElementType.XML_ELEMENT_NODE) {
- char[] aname = toString(curArg.name);
-
+ string aname = to!(string)(curArg.name);
+
if (aname == "Argument") {
ParsedType argType =
- parseType(toStringFree(xmlGetProp(curArg, "type")));
-
+ parseType(toStringFree(xmlGetProp(curArg, "type".dup.ptr)));
+
if (!first) {
couta ~= ", ";
dheada ~= ", ";
} else {
first = false;
}
-
+
couta ~= argType.CType;
dheada ~= argType.DType;
} else if (aname == "Ellipsis" && outputC) {
@@ -2017,17 +2015,17 @@ ParsedType parseType(char[] type)
} else {
first = false;
}
-
+
couta ~= "...";
dheada ~= "...";
}
}
}
-
+
cout ~= couta ~ ");\n";
dhead ~= dheada ~ ") _BCD_func_" ~ type ~ ";\n";
}
-
+
ParsedType pt;
if (nname != "MethodType") {
pt = new ParsedType("_BCD_func_" ~ type, "_BCD_func_" ~ type);
@@ -2037,63 +2035,63 @@ ParsedType parseType(char[] type)
pt = new ParsedType("_BCD_func_" ~ type, "bcd.bind.CXXDelegate");
pt.isFunction = true;
}
-
+
parsedCache[type] = pt;
} else if (nname == "Enumeration") {
if (parseThis(curNode, true)) parse_Enumeration(curNode);
-
+
// if this is fake, ignore it
- char[] aname = getNName(curNode);
-
+ string aname = getNName(curNode);
+
if (aname[0] == '.') {
parsedCache[type] = new ParsedType("int", "int");
break;
}
-
+
/* we need the demangled name in C, but there is no demangled
* for enumerations, so we need the parent */
if (!outputC) {
- char[] context = toStringFree(xmlGetProp(curNode, "context"));
+ string context = toStringFree(xmlGetProp(curNode, "context".dup.ptr));
if (context != "") {
ParsedType pt = parseType(context);
-
- pt.CType = replace(pt.CType, " *", "");
-
+
+ pt.CType = tr(pt.CType, " *", "");
+
if (pt.CType == "") {
pt.CType = "enum " ~ aname;
} else {
pt.CType = "enum " ~ pt.CType ~ "::" ~ aname;
}
pt.DType = "int";
-
+
parsedCache[type] = new ParsedType(pt);
break;
}
}
-
+
parsedCache[type] = new ParsedType("enum " ~ aname, "int");
-
+
} else if (nname == "Namespace") {
- char[] aname = toStringFree(xmlGetProp(curNode, "name"));
+ string aname = toStringFree(xmlGetProp(curNode, "name".dup.ptr));
if (aname == "::") aname = "";
parsedCache[type] = new ParsedType(aname, "");
-
+
} else {
parsedCache[type] = new ParsedType("void", "void");
writefln("I don't know how to parse the type %s.", nname);
}
-
+
break;
}
}
-
+
if (!(type in parsedCache)) {
parsedCache[type] = new ParsedType("void", "void");
writefln("Type %s not found!", type);
}
}
-
+
return parsedCache[type].dup();
}
@@ -2102,15 +2100,15 @@ ParsedType parseType(char[] type)
*/
void parse_Defines()
{
- File f = new File("out.i", FileMode.In);
+ File f = File("out.i");
bool inOurFile = false;
-
- bool[char[]] curDefines;
-
- while (!f.eof()) {
- char[] ln = f.readLine();
- char[][] lns = split(ln);
-
+
+ bool[string] curDefines;
+
+ foreach(char[] line; f.byLine ){
+ string ln = cast(string) line;
+ string[] lns = split(ln);
+
if (lns.length >= 1 &&
lns[0].length >= 1 &&
lns[0][0] == '#') {
@@ -2118,8 +2116,8 @@ void parse_Defines()
// this is a file specification
if (lns.length >= 3 &&
lns[2].length >= 2) {
- char[] fname = lns[2][1 .. lns[2].length - 1];
-
+ string fname = lns[2][1 .. lns[2].length - 1];
+
// if it's not in a file we should be parsing, don't output it
inOurFile = true;
if (outputAll) {
@@ -2129,39 +2127,39 @@ void parse_Defines()
if (fname != curFile) inOurFile = false;
}
}
-
+
} else if (lns[0] == "#define" && inOurFile) {
// turn the #define into a const int or const double
if (lns.length >= 3) {
if (isNumeric(lns[2])) {
curDefines[lns[1]] = true;
-
+
/* isNumeric can accept ending with 'L', but long is
* (usually) int, so strip it */
if (lns[2][$-1] == 'L') lns[2] = lns[2][0..$-1];
-
+
// int or double?
- if (find(lns[2], '.') != -1 ||
- find(lns[2], 'e') != -1 ||
- find(lns[2], 'E') != -1) {
+ if (lns[2] != "." ||
+ lns[2] != "e" ||
+ lns[2] != "E") {
dhead ~= "const double " ~ safeName(lns[1]) ~
" = " ~ lns[2] ~ ";\n";
} else {
dhead ~= "const int " ~ safeName(lns[1]) ~
" = " ~ lns[2] ~ ";\n";
}
-
+
} else if (lns[2].length >= 2 &&
lns[2][0] == '"' && lns[2][$-1] == '"') {
curDefines[lns[1]] = true;
-
+
// a constant string
- dhead ~= "const char[] " ~ safeName(lns[1]) ~
+ dhead ~= "const string " ~ safeName(lns[1]) ~
" = " ~ lns[2] ~ ";\n";
-
+
} else if (lns[2] in curDefines) {
curDefines[lns[1]] = true;
-
+
// could be #define'ing to something already #defined
dhead ~= "alias " ~ safeName(lns[2]) ~ " " ~ safeName(lns[1]) ~ ";\n";
}
@@ -2169,6 +2167,6 @@ void parse_Defines()
}
}
}
-
+
f.close();
}
|
|
|