alternative to assert
[gtkD.git] / wrap / utils / funct.d
blobcb2a72da17f14934608954e3a6761defc92ebfd3
1 /*
2 * This file is part of duit.
3 *
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.
8 *
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
19 module utils.funct;
21 //debug = Funct;
22 //debug = wrapType;
23 //debug = unwrapType;
24 //debug = mainStrct;
25 //debug = ctor;
26 //debug = type;
27 //debug = name;
28 //debug = parm;
29 //debug = externalDec
30 //debug = callback;
32 public struct Funct
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
42 char[] type;
43 char[] typeWrap;
44 char[] name;
45 char[] convName; /// name after convertion
46 // TODO parms need to be a struct
47 char[][] parmsType;
48 char[][] parmsWrap; /// this is the parameter wrapped type
49 char[][] parms;
50 char[][] parmsDesc;
51 bit[] parmConst;
52 bit[] parmPointer;
53 char[] typeDesc;
54 char[] strctVar;
55 char[] strctPointer;
58 /**
59 * Gets the type, name and parameters of the function
60 * Params:
61 * text = The Gtk API description of the function call
63 void init(char[] text, ConvParms* convParms)
65 strctVar = null;
66 getStrctVar(convParms);
67 strctPointer = convParms.strct.dup ~ '*';
68 debug(Funct) writefln("init text=%s", text);
69 int p = 0;
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,'(');
83 int countBrace = 0;
84 char[] currParmType;
85 char[] currParm;
86 debug(parm)writef("%s:", name);
87 while ( countBrace==0 && p<text.length && text[p]!=')' )
89 currParmType.length = 0;
90 currParm.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";
107 else
109 DuitClass.fixType(currParmType, p, text);
111 debug(parm)writefln(" -> %s", currParmType);
112 if ( currParmType != "..." )
114 currParm = DuitClass.until(p, text, "),");
116 else
118 currParm = "";
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);
140 else
142 strctVar = "";
145 return strctVar;
149 * Checks the type against the strcutWrap table
150 * Returns:
152 char[] getWrappedType(char[] currType, ConvParms* convParms)
154 char[] wType;
155 if ( currType == "gchar*" || currType == "char*" )
157 wType = "char[]";
159 else if ( convParms.structWrap.length == 0 )
161 // this should be the common case
162 wType = currType;
164 else if ( currType in convParms.structWrap )
166 wType = convParms.structWrap[currType];
167 debug(wrapType) writefln("WrappedType %s -> %s", currType, wType);
169 else
171 wType = currType;
174 debug(wrapType)writefln("\t wrapType: %s -> %s", currType, wType);
175 return wType;
179 * Gets the gtk from the Duit class to be used on the Gtk function call
180 * Params:
181 * currType =
182 * convParms =
183 * Returns:
185 char[] getUnwrappedType(char[] currType, ConvParms* convParms)
187 // TODO
188 return currType;
192 * Gets the external declaration
193 * Params:
194 * =
195 * aliases =
196 * Returns:
198 char[] getExternal(ConvParms* convParms, char[][char[]] aliases)
201 char[] ext = type
202 ~ " function("
203 ~ getParameters(convParms, aliases)
204 ~ ")"
205 ~ name
208 return ext;
211 char[] getParameters(ConvParms* convParms, char[][char[]] aliases)
213 char[] parameters;
215 int i=0;
216 while ( i<parmsType.length && i< parms.length )
219 if ( i>0 ) parameters ~= ", ";
220 if ( i>=0
221 && (parmsType[i]!="void" || parms[i].length>0)
224 parameters ~= parmsType[i] ~" "~DuitClass.idsToDuit(parms[i], convParms, aliases);
226 ++i;
229 return parameters;
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.
236 * Params:
237 * convParms =
238 * aliases =
239 * Returns:
241 char[] getCallbackParameters(int firstParameter, ConvParms* convParms, char[][char[]] aliases)
243 char[] parameters;
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";
265 else
267 parameters ~= parmsType[i] ~" "~DuitClass.idsToDuit(parms[i], convParms, aliases);
270 ++i;
273 return parameters;
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.
280 * Params:
281 * convParms =
282 * aliases =
283 * Returns:
285 char[] getCallbackVars(ConvParms* convParms, char[][char[]] aliases)
287 char[] parameters;
289 debug(callback)writefln("getCallbackVars "~convParms.clss);
290 debug(callback)writefln("\t %s", getExternal(convParms, aliases));
292 int i=1;
293 while ( i<parmsType.length && i< parms.length )
296 if ( i>1 ) parameters ~= ", ";
297 if ( i == parms.length-1 )
299 parameters ~= DuitClass.getClassVar(convParms);
301 else if ( i>=1
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)~")";
309 //else
311 //parameters ~= DuitClass.idsToDuit(parameterToDuit(i, convParms, aliases), convParms, aliases);
312 parameters ~= parameterToDuit(i, convParms, aliases);
315 ++i;
318 return parameters;
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(";
329 int pCount = 0;
330 foreach(int count, char[] parm ; parmsWrap )
332 if ( count >= firstParameter )
334 if ( pCount > 0 )
336 decl ~= ", ";
338 if ( count == parmsWrap.length-1 )
340 decl ~= convParms.clss;
342 else
344 decl ~= parm;
346 ++pCount;
349 decl ~= ")";
350 return decl;
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
358 * Params:
359 * strct =
360 * Returns:
362 char[] declaration(ConvParms* convParms, char[][char[]] aliases)
364 char[] dec;
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 (";
384 ctor = true;
385 convName = "this";
387 else
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~"(";
400 else
402 dec = "public static "~typeWrap~" "~convName~"(";
404 ctor = false;
406 int i=0;
407 int parmCount = 0;
408 while ( i<parmsType.length && i< parms.length )
410 if ( i == 0 )
412 debug(mainStrct)writefln("1st Parm %s ?= %s",parmsType[i], convParms.strct);
413 if ( parmsType[i] == strctPointer )
415 debug(mainStrct)writefln("\tSAME <<<<<<------");
416 --parmCount;
421 if ( parmCount>0 ) dec ~= ", ";
422 if ( parmCount>=0
423 && (parmsType[i]!="void" || parms[i].length>0)
426 dec ~= parmsWrap[i]~" "~DuitClass.idsToDuit(parms[i], convParms, aliases);
428 ++i;
429 ++parmCount;
431 dec ~= ')';
433 return dec;
437 * Wraps the parameters if necessary
438 * Params:
439 * i =
440 * Returns:
442 char[] parameterToGtk(int i, ConvParms* convParms, char[][char[]] aliases)
444 char[] parmToGtk;
445 if ( parmsType[i] != parmsWrap[i] )
447 if ( parmsWrap[i] == "char[]" )
449 parmToGtk = "Str.toStringz("
450 ~ DuitClass.idsToDuit(parms[i], convParms, aliases)
451 ~")";
453 else
455 char[] id = DuitClass.idsToDuit(parms[i], convParms, aliases);
456 parmToGtk = "("~id~" is null) ? null : "~id~ ".get"~parmsWrap[i]~"Struct()";
459 else
461 parmToGtk = DuitClass.idsToDuit(parms[i], convParms, aliases);
463 return parmToGtk;
466 char[] parameterToDuit(int i, ConvParms* convParms, char[][char[]] aliases)
468 char[] parmToDuit;
469 if ( parmsType[i] != parmsWrap[i] )
471 if ( parmsWrap[i] == "char[]" )
473 parmToDuit = "Str.toString("
474 ~ DuitClass.idsToDuit(parms[i], convParms, aliases)
475 ~")";
477 else
479 //parmToDuit = DuitClass.idsToDuit(parms[i], convParms, aliases);
480 parmToDuit = "new "~parmsWrap[i]~"("~DuitClass.idsToDuit(parms[i], convParms, aliases)~")";
483 else
485 parmToDuit = DuitClass.idsToDuit(parms[i], convParms, aliases);
487 return parmToDuit;
490 char[] getWrapParametersType()
492 char[] pw;
493 foreach ( int count, char[] typeW ; parmsWrap )
495 if ( count > 0 )
497 pw ~= ",";
499 pw ~= typeW;
501 return pw;
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)
512 char[][] bd;
514 //bd ~= "{";
516 char[] gtkCall;
518 debug(ctor)writefln("body ctor = %s",ctor);
520 if ( type != "void" )
522 if ( ctor )
524 gtkCall ~= "this(";
525 //if ( convParms.strct == "GObject" )
527 if ( convParms.realStrct.length > 0 )
529 gtkCall ~="cast("~convParms.realStrct~"*)";
531 else
533 gtkCall ~="cast("~convParms.strct~"*)";
538 else
540 gtkCall ~= "return ";
541 if ( type != typeWrap )
543 if ( typeWrap == "char[]" )
545 gtkCall ~= "Str.toString(";
547 else
549 gtkCall ~= "new "~typeWrap~"( ";
555 gtkCall ~= name ~ "(";
557 int i=0;
558 while ( i<parmsType.length && i< parms.length )
561 debug(parm) writefln("\t(%s -> %s) %s",parmsType[i], parmsWrap[i], parms[i]);
562 if ( i == 0 )
564 if ( parmsType[0]==strctPointer )
566 if ( convParms.templ.length == 0 )
568 gtkCall ~= DuitClass.toVar(convParms.strct.dup);
570 else
572 gtkCall ~= "get"~convParms.clss~"Struct()";
575 else if ( parms[0].length>0 )
577 gtkCall ~= parameterToGtk(0, convParms, aliases);
580 else
582 if ( parms[i].length>0 )
584 gtkCall ~= ", ";
585 gtkCall ~= parameterToGtk(i, convParms, aliases);
591 ++i;
594 if ( ctor )
596 gtkCall ~= ") ";
598 else if ( type != typeWrap )
600 gtkCall ~= ") ";
604 gtkCall ~= ");";
606 bd ~= gtkCall;
607 //bd ~= "}";
609 return bd;