2 * This file is part of duit.
4 * duit is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; either version 2.1 of the License, or
7 * (at your option) any later version.
9 * duit is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with duit; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
35 private import std
.string
;
36 private import std
.stdio
;
38 private import utils
.convparms
;
39 private import utils
.DuitClass
;
41 bit ctor
; /// when true this method was found to be a constructor
45 char[] convName
; /// name after convertion
46 // TODO parms need to be a struct
48 char[][] parmsWrap
; /// this is the parameter wrapped type
59 * Gets the type, name and parameters of the function
61 * text = The Gtk API description of the function call
63 void init(char[] text
, ConvParms
* convParms
)
66 getStrctVar(convParms
);
67 strctPointer
= convParms
.strct
.dup
~ '*';
68 debug(Funct
) writefln("init text=%s", text
);
70 DuitClass
.skipBlank(p
,text
);
71 type
= DuitClass
.untilBlank(p
, text
);
72 debug(type
)writef("type = %s", type
);
73 DuitClass
.fixType(type
, p
, text
);
74 debug(type
)writefln(" -> %s", type
);
75 DuitClass
.skipBlank(p
, text
);
76 name
= DuitClass
.until(p
, text
, '(');
78 DuitClass
.adjustTypeName(type
, name
);
80 typeWrap
= getWrappedType(type
.dup
, convParms
);
82 DuitClass
.skip(p
, text
,'(');
86 debug(parm
)writef("%s:", name
);
87 while ( countBrace
==0 && p
<text
.length
&& text
[p
]!=')' )
89 currParmType
.length
= 0;
92 DuitClass
.skipBlank(p
, text
);
93 currParmType
= DuitClass
.untilBlank(p
, text
, ",)");
94 DuitClass
.skipBlank(p
, text
);
95 debug(parm
)writef("currParmType = %s", currParmType
);
96 if ( std
.string
.find(" const volatile G_CONST_RETURN ", currParmType
) > 0 )
98 currParmType
= DuitClass
.untilBlank(p
, text
, ",)");
99 DuitClass
.skipBlank(p
, text
);
101 if ( "struct"==currParmType
)
103 currParmType
= DuitClass
.untilBlank(p
, text
, ",)");
104 DuitClass
.skipBlank(p
, text
);
105 currParmType
= "void";
109 DuitClass
.fixType(currParmType
, p
, text
);
111 debug(parm
)writefln(" -> %s", currParmType
);
112 if ( currParmType
!= "..." )
114 currParm
= DuitClass
.until(p
, text
, "),");
121 DuitClass
.adjustTypeName(currParmType
, currParm
);
123 parmsType
~= currParmType
.dup
;
124 parmsWrap
~= getWrappedType(currParmType
.dup
, convParms
);
125 parms
~= currParm
.dup
;
127 if ( p
<text
.length
&& text
[p
]==',') ++p
;
129 DuitClass
.skip(p
, text
, ';');
132 char[] getStrctVar(ConvParms
* convParms
)
134 if ( strctVar
.length
== 0 )
136 if ( convParms
.strct
.length
> 0 )
138 strctVar
= DuitClass
.toVar(convParms
.strct
.dup
);
149 * Checks the type against the strcutWrap table
152 char[] getWrappedType(char[] currType
, ConvParms
* convParms
)
155 if ( currType
== "gchar*" || currType
== "char*" )
159 else if ( convParms
.structWrap
.length
== 0 )
161 // this should be the common case
164 else if ( currType
in convParms
.structWrap
)
166 wType
= convParms
.structWrap
[currType
];
167 debug(wrapType
) writefln("WrappedType %s -> %s", currType
, wType
);
174 debug(wrapType
)writefln("\t wrapType: %s -> %s", currType
, wType
);
179 * Gets the gtk from the Duit class to be used on the Gtk function call
185 char[] getUnwrappedType(char[] currType
, ConvParms
* convParms
)
192 * Gets the external declaration
198 char[] getExternal(ConvParms
* convParms
, char[][char[]] aliases
)
203 ~ getParameters(convParms
, aliases
)
211 char[] getParameters(ConvParms
* convParms
, char[][char[]] aliases
)
216 while ( i
<parmsType
.length
&& i
< parms
.length
)
219 if ( i
>0 ) parameters
~= ", ";
221 && (parmsType
[i
]!="void" || parms
[i
].length
>0)
224 parameters
~= parmsType
[i
] ~" "~DuitClass
.idsToDuit(parms
[i
], convParms
, aliases
);
233 * Gets the parameters for the call back functions.
234 * These are all but the first parameter and the last parameter will be the class.
235 * I'm not sure ths is valid for all callbacks.
241 char[] getCallbackParameters(int firstParameter
, ConvParms
* convParms
, char[][char[]] aliases
)
245 debug(callback
)writefln("getCallbackParameters "~convParms
.clss
);
246 debug(callback
)writefln("\t %s", getExternal(convParms
, aliases
));
248 int i
=firstParameter
;
249 while ( i
<parmsType
.length
&& i
< parms
.length
)
252 if ( i
>firstParameter
) parameters
~= ", ";
253 if ( i
== parms
.length
-1 )
255 parameters
~= convParms
.clss
~" "~DuitClass
.getClassVar(convParms
);
257 else if ( i
>=firstParameter
258 && (parmsType
[i
]!="void" || parms
[i
].length
>0)
261 if ( i
== 0 && DuitClass
.endsWith(parmsType
[i
], '*') )
263 parameters
~= parmsType
[i
] ~" "~DuitClass
.idsToDuit(parms
[i
], convParms
, aliases
)~"Struct";
267 parameters
~= parmsType
[i
] ~" "~DuitClass
.idsToDuit(parms
[i
], convParms
, aliases
);
277 * Gets the actual parameters for the call back functions.
278 * These are all but the first parameter and the last parameter will be the class.
279 * I'm not sure ths is valid for all callbacks.
285 char[] getCallbackVars(ConvParms
* convParms
, char[][char[]] aliases
)
289 debug(callback
)writefln("getCallbackVars "~convParms
.clss
);
290 debug(callback
)writefln("\t %s", getExternal(convParms
, aliases
));
293 while ( i
<parmsType
.length
&& i
< parms
.length
)
296 if ( i
>1 ) parameters
~= ", ";
297 if ( i
== parms
.length
-1 )
299 parameters
~= DuitClass
.getClassVar(convParms
);
302 && (parmsType
[i
]!="void" || parms
[i
].length
>0)
305 //if ( parmsType[i]=="GtkWidget*" )
307 // parameters ~= "new Widget("~DuitClass.idsToDuit(parameterToDuit(i, convParms, aliases), convParms, aliases)~")";
311 //parameters ~= DuitClass.idsToDuit(parameterToDuit(i, convParms, aliases), convParms, aliases);
312 parameters
~= parameterToDuit(i
, convParms
, aliases
);
321 char[] getDelegateDeclaration(ConvParms
* convParms
, int firstParameter
= 0)
323 if ( DuitClass
.endsWith(typeWrap
,"user_function") )
325 typeWrap
= typeWrap
[0..typeWrap
.length
-13];
327 char[] decl
= typeWrap
~ " delegate(";
330 foreach(int count
, char[] parm
; parmsWrap
)
332 if ( count
>= firstParameter
)
338 if ( count
== parmsWrap
.length
-1 )
340 decl
~= convParms
.clss
;
355 * Gets the method header.
356 * If the type is a pointer to the main strcut and the name starts with "new"
357 * then the method is a constructor
362 char[] declaration(ConvParms
* convParms
, char[][char[]] aliases
)
365 debug(ctor
)writefln("declaration ctor strct = %s",convParms
.strct
);
366 debug(ctor
)writefln("declaration ctor realStrct = %s",convParms
.realStrct
);
367 debug(ctor
)writefln("declaration ctor type = %s",type
);
368 debug(ctor
)writefln("declaration ctor name = %s",name
);
369 convName
= DuitClass
.idsToDuit(name
, convParms
, aliases
);
370 if ( convParms
.strct
.length
>0
371 && DuitClass
.startsWith(convName
, "new")
372 && ( (type
== strctPointer
373 || type
== convParms
.ctorStrct
~"*")
374 ||
/* special GObject case */
375 (type
== "gpointer" && convParms
.strct
== "GObject")
376 ||
/* special Gtk... that return a GtkWidget pointer */
377 (type
== "GtkWidget*")
378 ||
/* special Gtk... that return a GtkWidget pointer */
379 (type
== "GtkObject*" && convParms
.strct
== "GtkAdjustment")
383 dec = "public this (";
389 if ( convName
== "new" )
391 convName
~= convParms
.outFile
;
393 if ( convParms
.strct
.length
>0
394 && parmsType
.length
> 0
395 && (parmsType
[0] == strctPointer
)
398 dec = "public "~typeWrap
~" "~convName
~"(";
402 dec = "public static "~typeWrap
~" "~convName
~"(";
408 while ( i
<parmsType
.length
&& i
< parms
.length
)
412 debug(mainStrct
)writefln("1st Parm %s ?= %s",parmsType
[i
], convParms
.strct
);
413 if ( parmsType
[i
] == strctPointer
)
415 debug(mainStrct
)writefln("\tSAME <<<<<<------");
421 if ( parmCount
>0 ) dec ~= ", ";
423 && (parmsType
[i
]!="void" || parms
[i
].length
>0)
426 dec ~= parmsWrap
[i
]~" "~DuitClass
.idsToDuit(parms
[i
], convParms
, aliases
);
437 * Wraps the parameters if necessary
442 char[] parameterToGtk(int i
, ConvParms
* convParms
, char[][char[]] aliases
)
445 if ( parmsType
[i
] != parmsWrap
[i
] )
447 if ( parmsWrap
[i
] == "char[]" )
449 parmToGtk
= "Str.toStringz("
450 ~ DuitClass
.idsToDuit(parms
[i
], convParms
, aliases
)
455 char[] id
= DuitClass
.idsToDuit(parms
[i
], convParms
, aliases
);
456 parmToGtk
= "("~id
~" is null) ? null : "~id
~ ".get"~parmsWrap
[i
]~"Struct()";
461 parmToGtk
= DuitClass
.idsToDuit(parms
[i
], convParms
, aliases
);
466 char[] parameterToDuit(int i
, ConvParms
* convParms
, char[][char[]] aliases
)
469 if ( parmsType
[i
] != parmsWrap
[i
] )
471 if ( parmsWrap
[i
] == "char[]" )
473 parmToDuit
= "Str.toString("
474 ~ DuitClass
.idsToDuit(parms
[i
], convParms
, aliases
)
479 //parmToDuit = DuitClass.idsToDuit(parms[i], convParms, aliases);
480 parmToDuit
= "new "~parmsWrap
[i
]~"("~DuitClass
.idsToDuit(parms
[i
], convParms
, aliases
)~")";
485 parmToDuit
= DuitClass
.idsToDuit(parms
[i
], convParms
, aliases
);
490 char[] getWrapParametersType()
493 foreach ( int count
, char[] typeW
; parmsWrap
)
505 * Gets the body of the Duit method.
506 * This include the call to the Gtk function.
507 * If the first parameter is a pointer to the struct make it implicit and use the internal struct
508 * Returns: The text of the body of the function NOT including the braces
510 char[][] bod(ConvParms
* convParms
, char[][char[]] aliases
)
518 debug(ctor
)writefln("body ctor = %s",ctor
);
520 if ( type
!= "void" )
525 //if ( convParms.strct == "GObject" )
527 if ( convParms
.realStrct
.length
> 0 )
529 gtkCall
~="cast("~convParms
.realStrct
~"*)";
533 gtkCall
~="cast("~convParms
.strct
~"*)";
540 gtkCall
~= "return ";
541 if ( type
!= typeWrap
)
543 if ( typeWrap
== "char[]" )
545 gtkCall
~= "Str.toString(";
549 gtkCall
~= "new "~typeWrap
~"( ";
555 gtkCall
~= name
~ "(";
558 while ( i
<parmsType
.length
&& i
< parms
.length
)
561 debug(parm
) writefln("\t(%s -> %s) %s",parmsType
[i
], parmsWrap
[i
], parms
[i
]);
564 if ( parmsType
[0]==strctPointer
)
566 if ( convParms
.templ
.length
== 0 )
568 gtkCall
~= DuitClass
.toVar(convParms
.strct
.dup
);
572 gtkCall
~= "get"~convParms
.clss
~"Struct()";
575 else if ( parms
[0].length
>0 )
577 gtkCall
~= parameterToGtk(0, convParms
, aliases
);
582 if ( parms
[i
].length
>0 )
585 gtkCall
~= parameterToGtk(i
, convParms
, aliases
);
598 else if ( type
!= typeWrap
)