Skip to content

Commit

Permalink
* Pyd now requires D 2.003 or later.
Browse files Browse the repository at this point in the history
* Pyd now compiles with D 2.003.
* Resolved long-standing string-copying annoyance (thanks to const).

git-svn-id: http://svn.dsource.org/projects/pyd/trunk@120 1df65b71-e716-0410-9316-ac55df2b1602
  • Loading branch information
KirkMcDonald authored and KirkMcDonald committed Jul 27, 2007
1 parent 20277a4 commit f36727b
Show file tree
Hide file tree
Showing 10 changed files with 318 additions and 285 deletions.
4 changes: 2 additions & 2 deletions examples/testdll/testdll.d
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ void foo(int i) {
writefln("You entered %s", i);
}

char[] bar(int i) {
string bar(int i) {
if (i > 10) {
return "It's greater than 10!";
} else {
return "It's less than 10!";
}
}

void baz(int i=10, char[] s="moo") {
void baz(int i=10, string s="moo") {
writefln("i = %s\ns = %s", i, s);
}

Expand Down
50 changes: 25 additions & 25 deletions infrastructure/meta/Demangle.d
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ enum MangledNameType
/*****************************************
* Pretty-prints a mangled type string.
*/
template demangleType(char[] str, MangledNameType wantQualifiedNames = MangledNameType.PrettyName)
template demangleType(string str, MangledNameType wantQualifiedNames = MangledNameType.PrettyName)
{
static if (wantQualifiedNames != MangledNameType.PrettyName) {
// There are only a few types where symbolnameof!(), qualifiednameof!()
Expand Down Expand Up @@ -69,7 +69,7 @@ template demangleType(char[] str, MangledNameType wantQualifiedNames = MangledNa

// split these off because they're numerous and simple
// Note: For portability, could replace "v" with void.mangleof, etc.
template demangleBasicType(char [] str)
template demangleBasicType(string str)
{
static if (str == "v") const char [] demangleBasicType = "void";
else static if (str == "b") const char [] demangleBasicType = "bool";
Expand Down Expand Up @@ -105,7 +105,7 @@ template demangleBasicType(char [] str)
else static assert(0, "Demangle Error: '" ~ str ~ "' is not a recognised basic type");
}

template demangleTypeConsumed(char [] str)
template demangleTypeConsumed(string str)
{
static if (str[0]=='A')
const int demangleTypeConsumed = 1 + demangleTypeConsumed!(str[1..$]);
Expand All @@ -131,7 +131,7 @@ template demangleTypeConsumed(char [] str)
// STATIC ARRAYS

// For static arrays, count number of digits used (eg, return 3 for "674")
template countLeadingDigits(char [] str)
template countLeadingDigits(string str)
{
static if (str.length>0 && beginsWithDigit!( str))
const int countLeadingDigits = 1 + countLeadingDigits!( str[1..$]);
Expand All @@ -146,7 +146,7 @@ template countLeadingDigits(char [] str)
// Sometimes the characters following the length are also digits!
// (this happens with templates, when the name being 'lengthed' is itself an Lname).
// We guard against this by ensuring that the L is less than the length of the string.
template getLname(char [] str)
template getLname(string str)
{
static if (str.length <= 9+1 || !beginsWithDigit!(str[1..$]) )
const char [] getLname = str[1..(str[0]-'0' + 1)];
Expand All @@ -162,7 +162,7 @@ template getLname(char [] str)

// Deal with the case where an Lname contains an embedded "__D".
// This can happen when classes, typedefs, etc are declared inside a function.
template pretty_Dname(char [] str, int dotnameconsumed, MangledNameType wantQualifiedNames)
template pretty_Dname(string str, int dotnameconsumed, MangledNameType wantQualifiedNames)
{
static if ( isMangledFunction!( (str[2+dotnameconsumed]))) {
const char [] pretty_Dname = pretty_Dfunction!(str, dotnameconsumed,
Expand All @@ -180,7 +180,7 @@ template pretty_Dname(char [] str, int dotnameconsumed, MangledNameType wantQual

// Deal with the case where an Lname contains an embedded ("__D") function.
// Split into a seperate function because it's so complicated.
template pretty_Dfunction(char [] str, int dotnameconsumed, int paramlistconsumed,
template pretty_Dfunction(string str, int dotnameconsumed, int paramlistconsumed,
MangledNameType wantQualifiedNames)
{
static if (wantQualifiedNames == MangledNameType.PrettyName) {
Expand All @@ -202,14 +202,14 @@ template pretty_Dfunction(char [] str, int dotnameconsumed, int paramlistconsume
}

// for an Lname that begins with "_D"
template get_DnameConsumed(char [] str)
template get_DnameConsumed(string str)
{
const int get_DnameConsumed = 2 + getQualifiedNameConsumed!(str[2..$])
+ demangleTypeConsumed!(str[2+getQualifedNameConsumed!(str[2..$])..$]);
}

// If Lname is a template, shows it as a template
template prettyLname(char [] str, MangledNameType wantQualifiedNames)
template prettyLname(string str, MangledNameType wantQualifiedNames)
{
static if (str.length>3 && str[0..3] == "__T") // Template instance name
static if (wantQualifiedNames == MangledNameType.PrettyName) {
Expand All @@ -230,7 +230,7 @@ template prettyLname(char [] str, MangledNameType wantQualifiedNames)

// str must start with an lname: first chars give the length
// how many chars are taken up with length digits + the name itself
template getLnameConsumed(char [] str)
template getLnameConsumed(string str)
{
static if (str.length==0)
const int getLnameConsumed=0;
Expand All @@ -244,7 +244,7 @@ template getLnameConsumed(char [] str)
const int getLnameConsumed = (str[0]-'0')*1000 + (str[1]-'0')*100 + (str[2]-'0')*10 + (str[3]-'0') + 4;
}

template getQualifiedName(char [] str, MangledNameType wantQualifiedNames, char [] dotstr = "")
template getQualifiedName(string str, MangledNameType wantQualifiedNames, string dotstr = "")
{
static if (str.length==0) const char [] getQualifiedName="";
// else static if (str.length>2 && str[0]=='_' && str[1]=='D')
Expand All @@ -268,7 +268,7 @@ template getQualifiedName(char [] str, MangledNameType wantQualifiedNames, char
}
}

template getQualifiedNameConsumed (char [] str)
template getQualifiedNameConsumed (string str)
{
static if ( str.length>1 && beginsWithDigit!(str) ) {
static if (getLnameConsumed!(str) < str.length && beginsWithDigit!( str[getLnameConsumed!(str)..$])) {
Expand All @@ -289,7 +289,7 @@ template getQualifiedNameConsumed (char [] str)
/* str[0] must indicate the extern linkage of the function. funcOrDelegStr is the name of the function,
* or "function " or "delegate "
*/
template demangleFunctionOrDelegate(char [] str, char [] funcOrDelegStr, MangledNameType wantQualifiedNames)
template demangleFunctionOrDelegate(string str, string funcOrDelegStr, MangledNameType wantQualifiedNames)
{
const char [] demangleFunctionOrDelegate = demangleExtern!(( str[0] ))
~ demangleReturnValue!(str[1..$], wantQualifiedNames)
Expand All @@ -300,7 +300,7 @@ template demangleFunctionOrDelegate(char [] str, char [] funcOrDelegStr, Mangled

// Special case: types that are in function parameters
// For function parameters, the type can also contain 'lazy', 'out' or 'inout'.
template demangleFunctionParamType(char[] str, MangledNameType wantQualifiedNames)
template demangleFunctionParamType(string str, MangledNameType wantQualifiedNames)
{
static if (str[0]=='L')
const char [] demangleFunctionParamType = "lazy " ~ demangleType!(str[1..$], wantQualifiedNames);
Expand All @@ -312,7 +312,7 @@ template demangleFunctionParamType(char[] str, MangledNameType wantQualifiedName
}

// Deal with 'out' and 'inout' parameters
template demangleFunctionParamTypeConsumed(char[] str)
template demangleFunctionParamTypeConsumed(string str)
{
static if (str[0]=='K' || str[0]=='J' || str[0]=='L')
const int demangleFunctionParamTypeConsumed = 1 + demangleTypeConsumed!(str[1..$]);
Expand All @@ -337,7 +337,7 @@ template demangleExtern(char c)

// Skip through the string until we find the return value. It can either be Z for normal
// functions, or Y for vararg functions.
template demangleReturnValue(char [] str, MangledNameType wantQualifiedNames)
template demangleReturnValue(string str, MangledNameType wantQualifiedNames)
{
static assert(str.length>=1, "Demangle error(Function): No return value found");
static if (str[0]=='Z' || str[0]=='Y' || str[0]=='X')
Expand All @@ -346,7 +346,7 @@ template demangleReturnValue(char [] str, MangledNameType wantQualifiedNames)
}

// Stop when we get to the return value
template demangleParamList(char [] str, MangledNameType wantQualifiedNames, char[] commastr = "")
template demangleParamList(string str, MangledNameType wantQualifiedNames, string commastr = "")
{
static if (str[0] == 'Z')
const char [] demangleParamList = "";
Expand All @@ -361,7 +361,7 @@ template demangleParamList(char [] str, MangledNameType wantQualifiedNames, char
}

// How many characters are used in the parameter list and return value
template demangleParamListAndRetValConsumed(char [] str)
template demangleParamListAndRetValConsumed(string str)
{
static assert (str.length>0, "Demangle error(ParamList): No return value found");
static if (str[0]=='Z' || str[0]=='Y' || str[0]=='X')
Expand All @@ -375,7 +375,7 @@ template demangleParamListAndRetValConsumed(char [] str)
// --------------------------------------------
// TEMPLATES

template templateValueArgConsumed(char [] str)
template templateValueArgConsumed(string str)
{
static if (str[0]=='n') const int templateValueArgConsumed = 1;
else static if (beginsWithDigit!(str)) const int templateValueArgConsumed = countLeadingDigits!(str);
Expand All @@ -386,7 +386,7 @@ template templateValueArgConsumed(char [] str)
}

// pretty-print a template value argument.
template prettyValueArg(char [] str)
template prettyValueArg(string str)
{
static if (str[0]=='n') const char [] prettyValueArg = "null";
else static if (beginsWithDigit!(str)) const char [] prettyValueArg = str;
Expand All @@ -397,7 +397,7 @@ template prettyValueArg(char [] str)
}

// Pretty-print a template argument
template prettyTemplateArg(char [] str, MangledNameType wantQualifiedNames)
template prettyTemplateArg(string str, MangledNameType wantQualifiedNames)
{
static if (str[0]=='S') // symbol name
const char [] prettyTemplateArg = prettyLname!(str[1..$], wantQualifiedNames);
Expand All @@ -410,7 +410,7 @@ template prettyTemplateArg(char [] str, MangledNameType wantQualifiedNames)
else static assert(0, "Unrecognised template argument type: {" ~ str ~ "}");
}

template templateArgConsumed(char [] str)
template templateArgConsumed(string str)
{
static if (str[0]=='S') // symbol name
const int templateArgConsumed = 1 + getLnameConsumed!(str[1..$]);
Expand All @@ -424,7 +424,7 @@ template templateArgConsumed(char [] str)

// Like function parameter lists, template parameter lists also end in a Z,
// but they don't have a return value at the end.
template prettyTemplateArgList(char [] str, MangledNameType wantQualifiedNames, char [] commastr="")
template prettyTemplateArgList(string str, MangledNameType wantQualifiedNames, string commastr="")
{
static if (str[0]=='Z')
const char[] prettyTemplateArgList = "";
Expand All @@ -434,7 +434,7 @@ template prettyTemplateArgList(char [] str, MangledNameType wantQualifiedNames,
~ prettyTemplateArgList!(str[templateArgConsumed!(str)..$], wantQualifiedNames, ", ");
}

template templateArgListConsumed(char [] str)
template templateArgListConsumed(string str)
{
static assert(str.length>0, "No Z found at end of template argument list");
static if (str[0]=='Z')
Expand All @@ -451,7 +451,7 @@ private {
* beginsWithDigit!(s) is equivalent to isdigit!((s[0]));
* it allows us to avoid the ugly double parentheses.
*/
template beginsWithDigit(char [] s)
template beginsWithDigit(string s)
{
static if (s[0]>='0' && s[0]<='9')
const bool beginsWithDigit = true;
Expand Down
52 changes: 26 additions & 26 deletions infrastructure/pyd/class_wrap.d
Original file line number Diff line number Diff line change
Expand Up @@ -275,30 +275,30 @@ fn_t = The type of the function. It is only useful to specify this
struct Def(alias fn) {
mixin _Def!(fn, symbolnameof!(fn), typeof(&fn), "");
}
struct Def(alias fn, char[] docstring) {
struct Def(alias fn, string docstring) {
mixin _Def!(fn, /*symbolnameof!(fn),*/ symbolnameof!(fn), typeof(&fn)/+, minArgs!(fn)+/, docstring);
}
struct Def(alias fn, char[] name, char[] docstring) {
struct Def(alias fn, string name, string docstring) {
mixin _Def!(fn, /*symbolnameof!(fn),*/ name, typeof(&fn)/+, minArgs!(fn)+/, docstring);
}
struct Def(alias fn, char[] name, fn_t) {
struct Def(alias fn, string name, fn_t) {
mixin _Def!(fn, /*symbolnameof!(fn),*/ name, fn_t/+, minArgs!(fn)+/, "");
}
struct Def(alias fn, fn_t) {
mixin _Def!(fn, /*symbolnameof!(fn),*/ symbolnameof!(fn), fn_t/+, minArgs!(fn)+/, "");
}
struct Def(alias fn, fn_t, char[] docstring) {
struct Def(alias fn, fn_t, string docstring) {
mixin _Def!(fn, /*symbolnameof!(fn),*/ symbolnameof!(fn), fn_t/+, minArgs!(fn)+/, docstring);
}
struct Def(alias fn, char[] name, fn_t, char[] docstring) {
struct Def(alias fn, string name, fn_t, string docstring) {
mixin _Def!(fn, /*symbolnameof!(fn),*/ name, fn_t/+, minArgs!(fn)+/, docstring);
}
/+
template Def(alias fn, char[] name, fn_t, uint MIN_ARGS=minArgs!(fn)/+, char[] docstring=""+/) {
template Def(alias fn, string name, fn_t, uint MIN_ARGS=minArgs!(fn)/+, string docstring=""+/) {
alias Def!(fn, /*symbolnameof!(fn),*/ name, fn_t, MIN_ARGS/+, docstring+/) Def;
}
+/
template _Def(alias fn, /*char[] _realname,*/ char[] name, fn_t/+, uint MIN_ARGS=minArgs!(fn)+/, char[] docstring) {
template _Def(alias fn, /*string _realname,*/ string name, fn_t/+, uint MIN_ARGS=minArgs!(fn)+/, string docstring) {
//static const type = ParamType.Def;
alias fn func;
alias fn_t func_t;
Expand Down Expand Up @@ -335,31 +335,31 @@ Wraps a static member function of the class. Identical to pyd.def.def
struct StaticDef(alias fn) {
mixin _StaticDef!(fn,/+ symbolnameof!(fn),+/ symbolnameof!(fn), typeof(&fn), minArgs!(fn), "");
}
struct StaticDef(alias fn, char[] docstring) {
struct StaticDef(alias fn, string docstring) {
mixin _StaticDef!(fn,/+ symbolnameof!(fn),+/ symbolnameof!(fn), typeof(&fn), minArgs!(fn), docstring);
}
struct StaticDef(alias _fn, char[] name, char[] docstring) {
struct StaticDef(alias _fn, string name, string docstring) {
mixin _StaticDef!(fn,/+ symbolnameof!(fn),+/ name, typeof(&fn), minArgs!(fn), docstring);
}
struct StaticDef(alias _fn, char[] name, fn_t, char[] docstring) {
struct StaticDef(alias _fn, string name, fn_t, string docstring) {
mixin _StaticDef!(fn,/+ symbolnameof!(fn),+/ name, fn_t, minArgs!(fn), docstring);
}
struct StaticDef(alias _fn, fn_t) {
mixin _StaticDef!(fn,/+ symbolnameof!(fn),+/ symbolnameof!(fn), fn_t, minArgs!(fn), "");
}
struct StaticDef(alias _fn, fn_t, char[] docstring) {
struct StaticDef(alias _fn, fn_t, string docstring) {
mixin _StaticDef!(fn,/+ symbolnameof!(fn),+/ symbolnameof!(fn), fn_t, minArgs!(fn), docstring);
}
struct StaticDef(alias _fn, char[] name, fn_t) {
struct StaticDef(alias _fn, string name, fn_t) {
mixin _StaticDef!(fn,/+ symbolnameof!(fn),+/ name, fn_t, minArgs!(fn), "");
}
struct StaticDef(alias _fn, char[] name, fn_t, uint MIN_ARGS) {
struct StaticDef(alias _fn, string name, fn_t, uint MIN_ARGS) {
mixin _StaticDef!(fn,/+ symbolnameof!(fn),+/ name, fn_t, MIN_ARGS, "");
}
struct StaticDef(alias _fn, char[] name, fn_t, uint MIN_ARGS, char[] docstring) {
struct StaticDef(alias _fn, string name, fn_t, uint MIN_ARGS, string docstring) {
mixin _StaticDef!(fn,/+ symbolnameof!(fn),+/ name, fn_t, MIN_ARGS, docstring);
}
template _StaticDef(alias fn,/+ char[] _realname,+/ char[] name, fn_t, uint MIN_ARGS, char[] docstring) {
template _StaticDef(alias fn,/+ string _realname,+/ string name, fn_t, uint MIN_ARGS, string docstring) {
//static const type = ParamType.StaticDef;
alias fn func;
alias fn_t func_t;
Expand Down Expand Up @@ -396,25 +396,25 @@ RO = Whether this is a read-only property.
struct Property(alias fn) {
mixin _Property!(fn, symbolnameof!(fn), symbolnameof!(fn), false, "");
}
struct Property(alias fn, char[] docstring) {
struct Property(alias fn, string docstring) {
mixin _Property!(fn, symbolnameof!(fn), symbolnameof!(fn), false, docstring);
}
struct Property(alias fn, char[] name, char[] docstring) {
struct Property(alias fn, string name, string docstring) {
mixin _Property!(fn, symbolnameof!(fn), name, false, docstring);
}
struct Property(alias fn, char[] name, bool RO) {
struct Property(alias fn, string name, bool RO) {
mixin _Property!(fn, symbolnameof!(fn), name, RO, "");
}
struct Property(alias fn, char[] name, bool RO, char[] docstring) {
struct Property(alias fn, string name, bool RO, string docstring) {
mixin _Property!(fn, symbolnameof!(fn), name, RO, docstring);
}
struct Property(alias fn, bool RO) {
mixin _Property!(fn, symbolnameof!(fn), symbolnameof!(fn), RO, "");
}
struct Property(alias fn, bool RO, char[] docstring) {
struct Property(alias fn, bool RO, string docstring) {
mixin _Property!(fn, symbolnameof!(fn), symbolnameof!(fn), RO, docstring);
}
template _Property(alias fn, char[] _realname, char[] name, bool RO, char[] docstring) {
template _Property(alias fn, string _realname, string name, bool RO, string docstring) {
alias property_parts!(fn).getter_type get_t;
alias property_parts!(fn).setter_type set_t;
static const char[] realname = _realname;
Expand Down Expand Up @@ -544,7 +544,7 @@ Exposes alternate iteration methods, originally intended for use with
D's delegate-as-iterator features, as methods returning a Python
iterator.
*/
struct AltIter(alias fn, char[] name = symbolnameof!(fn), iter_t = ParameterTypeTuple!(fn)[0]) {
struct AltIter(alias fn, string name = symbolnameof!(fn), iter_t = ParameterTypeTuple!(fn)[0]) {
static const bool needs_shim = false;
static void call(T) () {
static PyMethodDef empty = { null, null, 0, null };
Expand All @@ -563,15 +563,15 @@ struct AltIter(alias fn, char[] name = symbolnameof!(fn), iter_t = ParameterType

} /*Pyd_with_StackThreads*/

void wrap_class(T, Params...) (char[] docstring="", char[] modulename="") {
void wrap_class(T, Params...) (string docstring="", string modulename="") {
_wrap_class!(T, symbolnameof!(T), Params).wrap_class(docstring, modulename);
}
/+
template _wrap_class(T, Params...) {
mixin _wrap_class!(T, symbolnameof!(T), Params);
}
+/
template _wrap_class(_T, char[] name, Params...) {
template _wrap_class(_T, string name, Params...) {
static if (is(_T == class)) {
pragma(msg, "wrap_class: " ~ name);
mixin pyd.make_wrapper.make_wrapper!(_T, Params);
Expand All @@ -586,7 +586,7 @@ template _wrap_class(_T, char[] name, Params...) {
alias void shim_class;
alias _T* T;
}
void wrap_class(char[] docstring="", char[] modulename="") {
void wrap_class(string docstring="", string modulename="") {
pragma(msg, "shim.mangleof: " ~ shim_class.mangleof);
alias wrapped_class_type!(T) type;
//pragma(msg, "wrap_class, T is " ~ prettytypeof!(T));
Expand All @@ -602,7 +602,7 @@ void wrap_class(char[] docstring="", char[] modulename="") {
}

assert(Pyd_Module_p(modulename) !is null, "Must initialize module before wrapping classes.");
char[] module_name = toString(python.PyModule_GetName(Pyd_Module_p(modulename)));
string module_name = toString(python.PyModule_GetName(Pyd_Module_p(modulename)));

//////////////////
// Basic values //
Expand Down
Loading

0 comments on commit f36727b

Please sign in to comment.