Code cleanup in rna files (huge, higly automated with py script).
[blender.git] / source / blender / makesrna / intern / makesrna.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program 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 General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * Contributor(s): Blender Foundation (2008).
19  *
20  * ***** END GPL LICENSE BLOCK *****
21  */
22
23 /** \file blender/makesrna/intern/makesrna.c
24  *  \ingroup RNA
25  */
26
27
28 #include <float.h>
29 #include <limits.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <errno.h>
34
35 #include "MEM_guardedalloc.h"
36
37 #include "RNA_access.h"
38 #include "RNA_define.h"
39 #include "RNA_types.h"
40
41 #include "rna_internal.h"
42
43 #define RNA_VERSION_DATE "FIXME-RNA_VERSION_DATE"
44
45 #ifdef _WIN32
46 #  ifndef snprintf
47 #    define snprintf _snprintf
48 #  endif
49 #endif
50
51 /* so we can use __func__ everywhere */
52 #if defined(_MSC_VER)
53 #  define __func__ __FUNCTION__
54 #endif
55
56 /* Replace if different */
57 #define TMP_EXT ".tmp"
58
59
60 /* copied from BLI_file_older */
61 #include <sys/stat.h>
62 static int file_older(const char *file1, const char *file2)
63 {
64         struct stat st1, st2;
65         /* printf("compare: %s %s\n", file1, file2); */
66
67         if (stat(file1, &st1)) return 0;
68         if (stat(file2, &st2)) return 0;
69
70         return (st1.st_mtime < st2.st_mtime);
71 }
72 static const char *makesrna_path = NULL;
73
74 static int replace_if_different(char *tmpfile, const char *dep_files[])
75 {
76         /* return 0;  *//* use for testing had edited rna */
77
78 #define REN_IF_DIFF                                                           \
79         {                                                                         \
80                 FILE *file_test = fopen(orgfile, "rb");                                \
81                 if (file_test) {                                                       \
82                         fclose(file_test);                                                \
83                         if (fp_org) fclose(fp_org);                                        \
84                         if (fp_new) fclose(fp_new);                                        \
85                         if (remove(orgfile) != 0) {                                        \
86                                 fprintf(stderr, "%s:%d, Remove Error (%s): \"%s\"\n",         \
87                                         __FILE__, __LINE__, strerror(errno), orgfile);        \
88                                 return -1;                                                    \
89                         }                                                                 \
90                 }                                                                     \
91         }                                                                         \
92         if (rename(tmpfile, orgfile) != 0) {                                       \
93                 fprintf(stderr, "%s:%d, Rename Error (%s): \"%s\" -> \"%s\"\n",       \
94                         __FILE__, __LINE__, strerror(errno), tmpfile, orgfile);       \
95                 return -1;                                                            \
96         }                                                                         \
97         remove(tmpfile);                                                          \
98         return 1;                                                                 \
99
100 /* end REN_IF_DIFF */
101
102
103         FILE *fp_new = NULL, *fp_org = NULL;
104         int len_new, len_org;
105         char *arr_new, *arr_org;
106         int cmp;
107
108         char orgfile[4096];
109
110         strcpy(orgfile, tmpfile);
111         orgfile[strlen(orgfile) - strlen(TMP_EXT)] = '\0'; /* strip '.tmp' */
112
113         fp_org = fopen(orgfile, "rb");
114
115         if (fp_org == NULL) {
116                 REN_IF_DIFF;
117         }
118
119
120         /* XXX, trick to work around dependency problem
121          * assumes dep_files is in the same dir as makesrna.c, which is true for now. */
122
123         if (1) {
124                 /* first check if makesrna.c is newer then generated files
125                  * for development on makesrna.c you may want to disable this */
126                 if (file_older(orgfile, __FILE__)) {
127                         REN_IF_DIFF;
128                 }
129
130                 if (file_older(orgfile, makesrna_path)) {
131                         REN_IF_DIFF;
132                 }
133
134                 /* now check if any files we depend on are newer then any generated files */
135                 if (dep_files) {
136                         int pass;
137                         for (pass = 0; dep_files[pass]; pass++) {
138                                 char from_path[4096] = __FILE__;
139                                 char *p1, *p2;
140
141                                 /* dir only */
142                                 p1 = strrchr(from_path, '/');
143                                 p2 = strrchr(from_path, '\\');
144                                 strcpy((p1 > p2 ? p1 : p2)+1, dep_files[pass]);
145                                 /* account for build deps, if makesrna.c (this file) is newer */
146                                 if (file_older(orgfile, from_path)) {
147                                         REN_IF_DIFF;
148                                 }
149                         }
150                 }
151         }
152         /* XXX end dep trick */
153
154
155         fp_new = fopen(tmpfile, "rb");
156
157         if (fp_new == NULL) {
158                 /* shouldn't happen, just to be safe */
159                 fprintf(stderr, "%s:%d, open error: \"%s\"\n", __FILE__, __LINE__, tmpfile);
160                 fclose(fp_org);
161                 return -1;
162         }
163
164         fseek(fp_new, 0L, SEEK_END); len_new = ftell(fp_new); fseek(fp_new, 0L, SEEK_SET);
165         fseek(fp_org, 0L, SEEK_END); len_org = ftell(fp_org); fseek(fp_org, 0L, SEEK_SET);
166
167
168         if (len_new != len_org) {
169                 fclose(fp_new);
170                 fclose(fp_org);
171                 REN_IF_DIFF;
172         }
173
174         /* now compare the files... */
175         arr_new = MEM_mallocN(sizeof(char)*len_new, "rna_cmp_file_new");
176         arr_org = MEM_mallocN(sizeof(char)*len_org, "rna_cmp_file_org");
177
178         if (fread(arr_new, sizeof(char), len_new, fp_new) != len_new)
179                 fprintf(stderr, "%s:%d, error reading file %s for comparison.\n", __FILE__, __LINE__, tmpfile);
180         if (fread(arr_org, sizeof(char), len_org, fp_org) != len_org)
181                 fprintf(stderr, "%s:%d, error reading file %s for comparison.\n", __FILE__, __LINE__, orgfile);
182
183         fclose(fp_new);
184         fclose(fp_org);
185
186         cmp = memcmp(arr_new, arr_org, len_new);
187
188         MEM_freeN(arr_new);
189         MEM_freeN(arr_org);
190
191         if (cmp) {
192                 REN_IF_DIFF;
193         }
194         else {
195                 remove(tmpfile);
196                 return 0;
197         }
198
199 #undef REN_IF_DIFF
200 }
201
202 /* Helper to solve keyword problems with C/C++ */
203
204 static const char *rna_safe_id(const char *id)
205 {
206         if (strcmp(id, "default") == 0)
207                 return "default_value";
208         else if (strcmp(id, "operator") == 0)
209                 return "operator_value";
210
211         return id;
212 }
213
214 /* Sorting */
215
216 static int cmp_struct(const void *a, const void *b)
217 {
218         const StructRNA *structa = *(const StructRNA**)a;
219         const StructRNA *structb = *(const StructRNA**)b;
220
221         return strcmp(structa->identifier, structb->identifier);
222 }
223
224 static int cmp_property(const void *a, const void *b)
225 {
226         const PropertyRNA *propa = *(const PropertyRNA**)a;
227         const PropertyRNA *propb = *(const PropertyRNA**)b;
228
229         if (strcmp(propa->identifier, "rna_type") == 0) return -1;
230         else if (strcmp(propb->identifier, "rna_type") == 0) return 1;
231
232         if (strcmp(propa->identifier, "name") == 0) return -1;
233         else if (strcmp(propb->identifier, "name") == 0) return 1;
234
235         return strcmp(propa->name, propb->name);
236 }
237
238 static int cmp_def_struct(const void *a, const void *b)
239 {
240         const StructDefRNA *dsa = *(const StructDefRNA**)a;
241         const StructDefRNA *dsb = *(const StructDefRNA**)b;
242
243         return cmp_struct(&dsa->srna, &dsb->srna);
244 }
245
246 static int cmp_def_property(const void *a, const void *b)
247 {
248         const PropertyDefRNA *dpa = *(const PropertyDefRNA**)a;
249         const PropertyDefRNA *dpb = *(const PropertyDefRNA**)b;
250
251         return cmp_property(&dpa->prop, &dpb->prop);
252 }
253
254 static void rna_sortlist(ListBase *listbase, int(*cmp)(const void*, const void*))
255 {
256         Link *link;
257         void **array;
258         int a, size;
259         
260         if (listbase->first == listbase->last)
261                 return;
262
263         for (size = 0, link = listbase->first; link; link = link->next)
264                 size++;
265
266         array = MEM_mallocN(sizeof(void*)*size, "rna_sortlist");
267         for (a = 0, link = listbase->first; link; link = link->next, a++)
268                 array[a] = link;
269
270         qsort(array, size, sizeof(void*), cmp);
271
272         listbase->first = listbase->last = NULL;
273         for (a = 0; a<size; a++) {
274                 link = array[a];
275                 link->next = link->prev = NULL;
276                 rna_addtail(listbase, link);
277         }
278
279         MEM_freeN(array);
280 }
281
282 /* Preprocessing */
283
284 static void rna_print_c_string(FILE *f, const char *str)
285 {
286         static const char *escape[] = {"\''", "\"\"", "\??", "\\\\","\aa", "\bb", "\ff", "\nn", "\rr", "\tt", "\vv", NULL};
287         int i, j;
288
289         if (!str) {
290                 fprintf(f, "NULL");
291                 return;
292         }
293
294         fprintf(f, "\"");
295         for (i = 0; str[i]; i++) {
296                 for (j = 0; escape[j]; j++)
297                         if (str[i] == escape[j][0])
298                                 break;
299
300                 if (escape[j]) fprintf(f, "\\%c", escape[j][1]);
301                 else fprintf(f, "%c", str[i]);
302         }
303         fprintf(f, "\"");
304 }
305
306 static void rna_print_data_get(FILE *f, PropertyDefRNA *dp)
307 {
308         if (dp->dnastructfromname && dp->dnastructfromprop)
309                 fprintf(f, "    %s *data= (%s*)(((%s*)ptr->data)->%s);\n", dp->dnastructname, dp->dnastructname, dp->dnastructfromname, dp->dnastructfromprop);
310         else
311                 fprintf(f, "    %s *data= (%s*)(ptr->data);\n", dp->dnastructname, dp->dnastructname);
312 }
313
314 static void rna_print_id_get(FILE *f, PropertyDefRNA *dp)
315 {
316         fprintf(f, "    ID *id= ptr->id.data;\n");
317 }
318
319 static char *rna_alloc_function_name(const char *structname, const char *propname, const char *type)
320 {
321         AllocDefRNA *alloc;
322         char buffer[2048];
323         char *result;
324
325         snprintf(buffer, sizeof(buffer), "%s_%s_%s", structname, propname, type);
326         result = MEM_callocN(sizeof(char)*strlen(buffer)+1, "rna_alloc_function_name");
327         strcpy(result, buffer);
328
329         alloc = MEM_callocN(sizeof(AllocDefRNA), "AllocDefRNA");
330         alloc->mem = result;
331         rna_addtail(&DefRNA.allocs, alloc);
332
333         return result;
334 }
335
336 static StructRNA *rna_find_struct(const char *identifier)
337 {
338         StructDefRNA *ds;
339
340         for (ds = DefRNA.structs.first; ds; ds = ds->cont.next)
341                 if (strcmp(ds->srna->identifier, identifier) == 0)
342                         return ds->srna;
343
344         return NULL;
345 }
346
347 static const char *rna_find_type(const char *type)
348 {
349         StructDefRNA *ds;
350
351         for (ds = DefRNA.structs.first; ds; ds = ds->cont.next)
352                 if (ds->dnaname && strcmp(ds->dnaname, type) == 0)
353                         return ds->srna->identifier;
354
355         return NULL;
356 }
357
358 static const char *rna_find_dna_type(const char *type)
359 {
360         StructDefRNA *ds;
361
362         for (ds = DefRNA.structs.first; ds; ds = ds->cont.next)
363                 if (strcmp(ds->srna->identifier, type) == 0)
364                         return ds->dnaname;
365
366         return NULL;
367 }
368
369 static const char *rna_type_type_name(PropertyRNA *prop)
370 {
371         switch (prop->type) {
372                 case PROP_BOOLEAN:
373                 case PROP_INT:
374                 case PROP_ENUM:
375                         return "int";
376                 case PROP_FLOAT:
377                         return "float";
378                 case PROP_STRING:
379                         if (prop->flag & PROP_THICK_WRAP) {
380                                 return "char*";
381                         }
382                         else {
383                                 return "const char*";
384                         }
385                 default:
386                         return NULL;
387         }
388 }
389
390 static const char *rna_type_type(PropertyRNA *prop)
391 {
392         const char *type;
393
394         type = rna_type_type_name(prop);
395
396         if (type)
397                 return type;
398
399         return "PointerRNA";
400 }
401
402 static const char *rna_type_struct(PropertyRNA *prop)
403 {
404         const char *type;
405
406         type = rna_type_type_name(prop);
407
408         if (type)
409                 return "";
410
411         return "struct ";
412 }
413
414 static const char *rna_parameter_type_name(PropertyRNA *parm)
415 {
416         const char *type;
417
418         type = rna_type_type_name(parm);
419
420         if (type)
421                 return type;
422
423         switch (parm->type) {
424                 case PROP_POINTER:  {
425                         PointerPropertyRNA *pparm = (PointerPropertyRNA*)parm;
426
427                         if (parm->flag & PROP_RNAPTR)
428                                 return "PointerRNA";
429                         else
430                                 return rna_find_dna_type((const char *)pparm->type);
431                 }
432                 case PROP_COLLECTION: {
433                         return "ListBase";
434                 }
435                 default:
436                         return "<error, no type specified>";
437         }
438 }
439
440 static int rna_enum_bitmask(PropertyRNA *prop)
441 {
442         EnumPropertyRNA *eprop = (EnumPropertyRNA*)prop;
443         int a, mask = 0;
444
445         if (eprop->item) {
446                 for (a = 0; a<eprop->totitem; a++)
447                         if (eprop->item[a].identifier[0])
448                                 mask |= eprop->item[a].value;
449         }
450         
451         return mask;
452 }
453
454 static int rna_color_quantize(PropertyRNA *prop, PropertyDefRNA *dp)
455 {
456         return ( (prop->type == PROP_FLOAT) &&
457                  (prop->subtype == PROP_COLOR || prop->subtype == PROP_COLOR_GAMMA) &&
458                  (IS_DNATYPE_FLOAT_COMPAT(dp->dnatype) == 0) );
459 }
460
461 static const char *rna_function_string(void *func)
462 {
463         return (func)? (const char*)func: "NULL";
464 }
465
466 static void rna_float_print(FILE *f, float num)
467 {
468         if (num == -FLT_MAX) fprintf(f, "-FLT_MAX");
469         else if (num == FLT_MAX) fprintf(f, "FLT_MAX");
470         else if ((int)num == num) fprintf(f, "%.1ff", num);
471         else fprintf(f, "%.10ff", num);
472 }
473
474 static void rna_int_print(FILE *f, int num)
475 {
476         if (num == INT_MIN) fprintf(f, "INT_MIN");
477         else if (num == INT_MAX) fprintf(f, "INT_MAX");
478         else fprintf(f, "%d", num);
479 }
480
481 static char *rna_def_property_get_func(FILE *f, StructRNA *srna, PropertyRNA *prop, PropertyDefRNA *dp, const char *manualfunc)
482 {
483         char *func;
484
485         if (prop->flag & PROP_IDPROPERTY && manualfunc == NULL)
486                 return NULL;
487
488         if (!manualfunc) {
489                 if (!dp->dnastructname || !dp->dnaname) {
490                         fprintf(stderr, "%s (0): %s.%s has no valid dna info.\n",
491                                 __func__, srna->identifier, prop->identifier);
492                         DefRNA.error = 1;
493                         return NULL;
494                 }
495
496                 /* typecheck,  */
497                 if (dp->dnatype && *dp->dnatype) {
498
499                         if (prop->type == PROP_FLOAT) {
500                                 if (IS_DNATYPE_FLOAT_COMPAT(dp->dnatype) == 0) {
501                                         if (prop->subtype != PROP_COLOR_GAMMA) { /* colors are an exception. these get translated */
502                                                 fprintf(stderr, "%s (1): %s.%s is a '%s' but wrapped as type '%s'.\n",
503                                                         __func__, srna->identifier, prop->identifier, dp->dnatype, RNA_property_typename(prop->type));
504                                                 DefRNA.error = 1;
505                                                 return NULL;
506                                         }
507                                 }
508                         }
509                         else if (prop->type == PROP_INT || prop->type == PROP_BOOLEAN || prop->type == PROP_ENUM) {
510                                 if (IS_DNATYPE_INT_COMPAT(dp->dnatype) == 0) {
511                                         fprintf(stderr, "%s (2): %s.%s is a '%s' but wrapped as type '%s'.\n",
512                                                 __func__, srna->identifier, prop->identifier, dp->dnatype, RNA_property_typename(prop->type));
513                                         DefRNA.error = 1;
514                                         return NULL;
515                                 }
516                         }
517                 }
518
519         }
520
521         func = rna_alloc_function_name(srna->identifier, rna_safe_id(prop->identifier), "get");
522
523         switch (prop->type) {
524                 case PROP_STRING: {
525                         StringPropertyRNA *sprop = (StringPropertyRNA*)prop;
526                         fprintf(f, "void %s(PointerRNA *ptr, char *value)\n", func);
527                         fprintf(f, "{\n");
528                         if (manualfunc) {
529                                 fprintf(f, "    %s(ptr, value);\n", manualfunc);
530                         }
531                         else {
532                                 const PropertySubType subtype = prop->subtype;
533                                 const char *string_copy_func = (subtype == PROP_FILEPATH ||
534                                                                subtype == PROP_DIRPATH  ||
535                                                                subtype == PROP_FILENAME ||
536                                                                subtype == PROP_BYTESTRING) ?
537                                             "BLI_strncpy" : "BLI_strncpy_utf8";
538
539                                 rna_print_data_get(f, dp);
540                                 if (sprop->maxlength)
541                                         fprintf(f, "    %s(value, data->%s, %d);\n", string_copy_func, dp->dnaname, sprop->maxlength);
542                                 else
543                                         fprintf(f, "    %s(value, data->%s, sizeof(data->%s));\n", string_copy_func, dp->dnaname, dp->dnaname);
544                         }
545                         fprintf(f, "}\n\n");
546                         break;
547                 }
548                 case PROP_POINTER: {
549                         fprintf(f, "PointerRNA %s(PointerRNA *ptr)\n", func);
550                         fprintf(f, "{\n");
551                         if (manualfunc) {
552                                 fprintf(f, "    return %s(ptr);\n", manualfunc);
553                         }
554                         else {
555                                 PointerPropertyRNA *pprop = (PointerPropertyRNA*)prop;
556                                 rna_print_data_get(f, dp);
557                                 if (dp->dnapointerlevel == 0)
558                                         fprintf(f, "    return rna_pointer_inherit_refine(ptr, &RNA_%s, &data->%s);\n", (const char*)pprop->type, dp->dnaname);
559                                 else
560                                         fprintf(f, "    return rna_pointer_inherit_refine(ptr, &RNA_%s, data->%s);\n", (const char*)pprop->type, dp->dnaname);
561                         }
562                         fprintf(f, "}\n\n");
563                         break;
564                 }
565                 case PROP_COLLECTION: {
566                         CollectionPropertyRNA *cprop = (CollectionPropertyRNA*)prop;
567
568                         fprintf(f, "static PointerRNA %s(CollectionPropertyIterator *iter)\n", func);
569                         fprintf(f, "{\n");
570                         if (manualfunc) {
571                                 if (strcmp(manualfunc, "rna_iterator_listbase_get") == 0 ||
572                                    strcmp(manualfunc, "rna_iterator_array_get") == 0 ||
573                                    strcmp(manualfunc, "rna_iterator_array_dereference_get") == 0)
574                                         fprintf(f, "    return rna_pointer_inherit_refine(&iter->parent, &RNA_%s, %s(iter));\n", (cprop->item_type)? (const char*)cprop->item_type: "UnknownType", manualfunc);
575                                 else
576                                         fprintf(f, "    return %s(iter);\n", manualfunc);
577                         }
578                         fprintf(f, "}\n\n");
579                         break;
580                 }
581                 default:
582                         if (prop->arraydimension) {
583                                 if (prop->flag & PROP_DYNAMIC)
584                                         fprintf(f, "void %s(PointerRNA *ptr, %s values[])\n", func, rna_type_type(prop));
585                                 else
586                                         fprintf(f, "void %s(PointerRNA *ptr, %s values[%u])\n", func, rna_type_type(prop), prop->totarraylength);
587                                 fprintf(f, "{\n");
588
589                                 if (manualfunc) {
590                                         fprintf(f, "    %s(ptr, values);\n", manualfunc);
591                                 }
592                                 else {
593                                         rna_print_data_get(f, dp);
594
595                                         if (prop->flag & PROP_DYNAMIC) {
596                                                 char *lenfunc = rna_alloc_function_name(srna->identifier, rna_safe_id(prop->identifier), "get_length");
597                                                 fprintf(f, "    int i, arraylen[RNA_MAX_ARRAY_DIMENSION];\n");
598                                                 fprintf(f, "    int len= %s(ptr, arraylen);\n\n", lenfunc);
599                                                 fprintf(f, "    for(i=0; i<len; i++) {\n");
600                                                 MEM_freeN(lenfunc);
601                                         }
602                                         else {
603                                                 fprintf(f, "    int i;\n\n");
604                                                 fprintf(f, "    for(i=0; i<%u; i++) {\n", prop->totarraylength);
605                                         }
606
607                                         if (dp->dnaarraylength == 1) {
608                                                 if (prop->type == PROP_BOOLEAN && dp->booleanbit)
609                                                         fprintf(f, "            values[i]= %s((data->%s & (%d<<i)) != 0);\n", (dp->booleannegative)? "!": "", dp->dnaname, dp->booleanbit);
610                                                 else
611                                                         fprintf(f, "            values[i]= (%s)%s((&data->%s)[i]);\n", rna_type_type(prop), (dp->booleannegative)? "!": "", dp->dnaname);
612                                         }
613                                         else {
614                                                 if (prop->type == PROP_BOOLEAN && dp->booleanbit) {
615                                                         fprintf(f, "            values[i]= %s((data->%s[i] & ", (dp->booleannegative)? "!": "", dp->dnaname);
616                                                         rna_int_print(f, dp->booleanbit);
617                                                         fprintf(f, ") != 0);\n");
618                                                 }
619                                                 else if (rna_color_quantize(prop, dp))
620                                                         fprintf(f, "            values[i]= (%s)(data->%s[i]*(1.0f/255.0f));\n", rna_type_type(prop), dp->dnaname);
621                                                 else if (dp->dnatype)
622                                                         fprintf(f, "            values[i]= (%s)%s(((%s*)data->%s)[i]);\n", rna_type_type(prop), (dp->booleannegative)? "!": "", dp->dnatype, dp->dnaname);
623                                                 else
624                                                         fprintf(f, "            values[i]= (%s)%s((data->%s)[i]);\n", rna_type_type(prop), (dp->booleannegative)? "!": "", dp->dnaname);
625                                         }
626                                         fprintf(f, "    }\n");
627                                 }
628                                 fprintf(f, "}\n\n");
629                         }
630                         else {
631                                 fprintf(f, "%s %s(PointerRNA *ptr)\n", rna_type_type(prop), func);
632                                 fprintf(f, "{\n");
633
634                                 if (manualfunc) {
635                                         fprintf(f, "    return %s(ptr);\n", manualfunc);
636                                 }
637                                 else {
638                                         rna_print_data_get(f, dp);
639                                         if (prop->type == PROP_BOOLEAN && dp->booleanbit) {
640                                                 fprintf(f, "    return %s(((data->%s) & ", (dp->booleannegative)? "!": "", dp->dnaname);
641                                                 rna_int_print(f, dp->booleanbit);
642                                                 fprintf(f, ") != 0);\n");
643                                         }
644                                         else if (prop->type == PROP_ENUM && dp->enumbitflags) {
645                                                 fprintf(f, "    return ((data->%s) & ", dp->dnaname);
646                                                 rna_int_print(f, rna_enum_bitmask(prop));
647                                                 fprintf(f, ");\n");
648                                         }
649                                         else
650                                                 fprintf(f, "    return (%s)%s(data->%s);\n", rna_type_type(prop), (dp->booleannegative)? "!": "", dp->dnaname);
651                                 }
652
653                                 fprintf(f, "}\n\n");
654                         }
655                         break;
656         }
657
658         return func;
659 }
660
661 /* defined min/max variables to be used by rna_clamp_value() */
662 static void rna_clamp_value_range(FILE *f, PropertyRNA *prop)
663 {
664         if (prop->type == PROP_FLOAT) {
665                 FloatPropertyRNA *fprop = (FloatPropertyRNA*)prop;
666                 if (fprop->range) {
667                         fprintf(f, "    float prop_clamp_min, prop_clamp_max;\n");
668                         fprintf(f, "    %s(ptr, &prop_clamp_min, &prop_clamp_max);\n", rna_function_string(fprop->range));
669                 }
670         }
671         else if (prop->type == PROP_INT) {
672                 IntPropertyRNA *iprop = (IntPropertyRNA*)prop;
673                 if (iprop->range) {
674                         fprintf(f, "    int prop_clamp_min, prop_clamp_max;\n");
675                         fprintf(f, "    %s(ptr, &prop_clamp_min, &prop_clamp_max);\n", rna_function_string(iprop->range));
676                 }
677         }
678 }
679
680 static void rna_clamp_value(FILE *f, PropertyRNA *prop, int array)
681 {
682         if (prop->type == PROP_INT) {
683                 IntPropertyRNA *iprop = (IntPropertyRNA*)prop;
684
685                 if (iprop->hardmin != INT_MIN || iprop->hardmax != INT_MAX) {
686                         if (array) fprintf(f, "CLAMPIS(values[i], ");
687                         else fprintf(f, "CLAMPIS(value, ");
688                         if (iprop->range) {
689                                 fprintf(f, "prop_clamp_min, prop_clamp_max);");
690                         }
691                         else {
692                                 rna_int_print(f, iprop->hardmin); fprintf(f, ", ");
693                                 rna_int_print(f, iprop->hardmax); fprintf(f, ");\n");
694                         }
695                         return;
696                 }
697         }
698         else if (prop->type == PROP_FLOAT) {
699                 FloatPropertyRNA *fprop = (FloatPropertyRNA*)prop;
700
701                 if (fprop->hardmin != -FLT_MAX || fprop->hardmax != FLT_MAX) {
702                         if (array) fprintf(f, "CLAMPIS(values[i], ");
703                         else fprintf(f, "CLAMPIS(value, ");
704                         if (fprop->range) {
705                                 fprintf(f, "prop_clamp_min, prop_clamp_max);");
706                         }
707                         else {
708                                 rna_float_print(f, fprop->hardmin); fprintf(f, ", ");
709                                 rna_float_print(f, fprop->hardmax); fprintf(f, ");\n");
710                         }
711                         return;
712                 }
713         }
714
715         if (array)
716                 fprintf(f, "values[i];\n");
717         else
718                 fprintf(f, "value;\n");
719 }
720
721 static char *rna_def_property_set_func(FILE *f, StructRNA *srna, PropertyRNA *prop, PropertyDefRNA *dp, const char *manualfunc)
722 {
723         char *func;
724
725         if (!(prop->flag & PROP_EDITABLE))
726                 return NULL;
727         if (prop->flag & PROP_IDPROPERTY && manualfunc == NULL)
728                 return NULL;
729
730         if (!manualfunc) {
731                 if (!dp->dnastructname || !dp->dnaname) {
732                         if (prop->flag & PROP_EDITABLE) {
733                                 fprintf(stderr, "%s: %s.%s has no valid dna info.\n",
734                                         __func__, srna->identifier, prop->identifier);
735                                 DefRNA.error = 1;
736                         }
737                         return NULL;
738                 }
739         }
740
741         func = rna_alloc_function_name(srna->identifier, rna_safe_id(prop->identifier), "set");
742
743         switch (prop->type) {
744                 case PROP_STRING: {
745                         StringPropertyRNA *sprop = (StringPropertyRNA*)prop;
746                         fprintf(f, "void %s(PointerRNA *ptr, const char *value)\n", func);
747                         fprintf(f, "{\n");
748                         if (manualfunc) {
749                                 fprintf(f, "    %s(ptr, value);\n", manualfunc);
750                         }
751                         else {
752                                 const PropertySubType subtype = prop->subtype;
753                                 const char *string_copy_func = (subtype == PROP_FILEPATH ||
754                                                                subtype == PROP_DIRPATH  ||
755                                                                subtype == PROP_FILENAME ||
756                                                                subtype == PROP_BYTESTRING) ?
757                                             "BLI_strncpy" : "BLI_strncpy_utf8";
758
759                                 rna_print_data_get(f, dp);
760                                 if (sprop->maxlength)
761                                         fprintf(f, "    %s(data->%s, value, %d);\n", string_copy_func, dp->dnaname, sprop->maxlength);
762                                 else
763                                         fprintf(f, "    %s(data->%s, value, sizeof(data->%s));\n", string_copy_func, dp->dnaname, dp->dnaname);
764                         }
765                         fprintf(f, "}\n\n");
766                         break;
767                 }
768                 case PROP_POINTER: {
769                         fprintf(f, "void %s(PointerRNA *ptr, PointerRNA value)\n", func);
770                         fprintf(f, "{\n");
771                         if (manualfunc) {
772                                 fprintf(f, "    %s(ptr, value);\n", manualfunc);
773                         }
774                         else {
775                                 rna_print_data_get(f, dp);
776
777                                 if (prop->flag & PROP_ID_SELF_CHECK) {
778                                         rna_print_id_get(f, dp);
779                                         fprintf(f, "    if(id==value.data) return;\n\n");
780                                 }
781
782                                 if (prop->flag & PROP_ID_REFCOUNT) {
783                                         fprintf(f, "\n  if(data->%s)\n", dp->dnaname);
784                                         fprintf(f, "            id_us_min((ID*)data->%s);\n", dp->dnaname);
785                                         fprintf(f, "    if(value.data)\n");
786                                         fprintf(f, "            id_us_plus((ID*)value.data);\n\n");
787                                 }
788                                 else {
789                                         PointerPropertyRNA *pprop = (PointerPropertyRNA*)dp->prop;
790                                         StructRNA *type = rna_find_struct((const char*)pprop->type);
791                                         if (type && (type->flag & STRUCT_ID)) {
792                                                 fprintf(f, "    if(value.data)\n");
793                                                 fprintf(f, "            id_lib_extern((ID*)value.data);\n\n");
794                                         }
795                                 }
796
797                                 fprintf(f, "    data->%s= value.data;\n", dp->dnaname);
798
799                         }
800                         fprintf(f, "}\n\n");
801                         break;
802                 }
803                 default:
804                         if (prop->arraydimension) {
805                                 if (prop->flag & PROP_DYNAMIC)
806                                         fprintf(f, "void %s(PointerRNA *ptr, const %s values[])\n", func, rna_type_type(prop));
807                                 else
808                                         fprintf(f, "void %s(PointerRNA *ptr, const %s values[%u])\n", func, rna_type_type(prop), prop->totarraylength);
809                                 fprintf(f, "{\n");
810
811                                 if (manualfunc) {
812                                         fprintf(f, "    %s(ptr, values);\n", manualfunc);
813                                 }
814                                 else {
815                                         rna_print_data_get(f, dp);
816
817                                         if (prop->flag & PROP_DYNAMIC) {
818                                                 char *lenfunc = rna_alloc_function_name(srna->identifier, rna_safe_id(prop->identifier), "set_length");
819                                                 fprintf(f, "    int i, arraylen[RNA_MAX_ARRAY_DIMENSION];\n");
820                                                 fprintf(f, "    int len= %s(ptr, arraylen);\n\n", lenfunc);
821                                                 rna_clamp_value_range(f, prop);
822                                                 fprintf(f, "    for(i=0; i<len; i++) {\n");
823                                                 MEM_freeN(lenfunc);
824                                         }
825                                         else {
826                                                 fprintf(f, "    int i;\n\n");
827                                                 rna_clamp_value_range(f, prop);
828                                                 fprintf(f, "    for(i=0; i<%u; i++) {\n", prop->totarraylength);
829                                         }
830
831                                         if (dp->dnaarraylength == 1) {
832                                                 if (prop->type == PROP_BOOLEAN && dp->booleanbit) {
833                                                         fprintf(f, "            if(%svalues[i]) data->%s |= (%d<<i);\n", (dp->booleannegative)? "!": "", dp->dnaname, dp->booleanbit);
834                                                         fprintf(f, "            else data->%s &= ~(%d<<i);\n", dp->dnaname, dp->booleanbit);
835                                                 }
836                                                 else {
837                                                         fprintf(f, "            (&data->%s)[i]= %s", dp->dnaname, (dp->booleannegative)? "!": "");
838                                                         rna_clamp_value(f, prop, 1);
839                                                 }
840                                         }
841                                         else {
842                                                 if (prop->type == PROP_BOOLEAN && dp->booleanbit) {
843                                                         fprintf(f, "            if(%svalues[i]) data->%s[i] |= ", (dp->booleannegative)? "!": "", dp->dnaname);
844                                                         rna_int_print(f, dp->booleanbit);
845                                                         fprintf(f, ";\n");
846                                                         fprintf(f, "            else data->%s[i] &= ~", dp->dnaname);
847                                                         rna_int_print(f, dp->booleanbit);
848                                                         fprintf(f, ";\n");
849                                                 }
850                                                 else if (rna_color_quantize(prop, dp)) {
851                                                         fprintf(f, "            data->%s[i]= FTOCHAR(values[i]);\n", dp->dnaname);
852                                                 }
853                                                 else {
854                                                         if (dp->dnatype)
855                                                                 fprintf(f, "            ((%s*)data->%s)[i]= %s", dp->dnatype, dp->dnaname, (dp->booleannegative)? "!": "");
856                                                         else
857                                                                 fprintf(f, "            (data->%s)[i]= %s", dp->dnaname, (dp->booleannegative)? "!": "");
858                                                         rna_clamp_value(f, prop, 1);
859                                                 }
860                                         }
861                                         fprintf(f, "    }\n");
862                                 }
863                                 fprintf(f, "}\n\n");
864                         }
865                         else {
866                                 fprintf(f, "void %s(PointerRNA *ptr, %s value)\n", func, rna_type_type(prop));
867                                 fprintf(f, "{\n");
868
869                                 if (manualfunc) {
870                                         fprintf(f, "    %s(ptr, value);\n", manualfunc);
871                                 }
872                                 else {
873                                         rna_print_data_get(f, dp);
874                                         if (prop->type == PROP_BOOLEAN && dp->booleanbit) {
875                                                 fprintf(f, "    if(%svalue) data->%s |= ", (dp->booleannegative)? "!": "", dp->dnaname);
876                                                 rna_int_print(f, dp->booleanbit);
877                                                 fprintf(f, ";\n");
878                                                 fprintf(f, "    else data->%s &= ~", dp->dnaname);
879                                                 rna_int_print(f, dp->booleanbit);
880                                                 fprintf(f, ";\n");
881                                         }
882                                         else if (prop->type == PROP_ENUM && dp->enumbitflags) {
883                                                 fprintf(f, "    data->%s &= ~", dp->dnaname);
884                                                 rna_int_print(f, rna_enum_bitmask(prop));
885                                                 fprintf(f, ";\n");
886                                                 fprintf(f, "    data->%s |= value;\n", dp->dnaname);
887                                         }
888                                         else {
889                                                 rna_clamp_value_range(f, prop);
890                                                 fprintf(f, "    data->%s= %s", dp->dnaname, (dp->booleannegative)? "!": "");
891                                                 rna_clamp_value(f, prop, 0);
892                                         }
893                                 }
894                                 fprintf(f, "}\n\n");
895                         }
896                         break;
897         }
898
899         return func;
900 }
901
902 static char *rna_def_property_length_func(FILE *f, StructRNA *srna, PropertyRNA *prop, PropertyDefRNA *dp, const char *manualfunc)
903 {
904         char *func = NULL;
905
906         if (prop->flag & PROP_IDPROPERTY && manualfunc == NULL)
907                 return NULL;
908
909         if (prop->type == PROP_STRING) {
910                 if (!manualfunc) {
911                         if (!dp->dnastructname || !dp->dnaname) {
912                                 fprintf(stderr, "%s: %s.%s has no valid dna info.\n",
913                                         __func__, srna->identifier, prop->identifier);
914                                 DefRNA.error = 1;
915                                 return NULL;
916                         }
917                 }
918
919                 func = rna_alloc_function_name(srna->identifier, rna_safe_id(prop->identifier), "length");
920
921                 fprintf(f, "int %s(PointerRNA *ptr)\n", func);
922                 fprintf(f, "{\n");
923                 if (manualfunc) {
924                         fprintf(f, "    return %s(ptr);\n", manualfunc);
925                 }
926                 else {
927                         rna_print_data_get(f, dp);
928                         fprintf(f, "    return strlen(data->%s);\n", dp->dnaname);
929                 }
930                 fprintf(f, "}\n\n");
931         }
932         else if (prop->type == PROP_COLLECTION) {
933                 if (!manualfunc) {
934                         if (prop->type == PROP_COLLECTION && (!(dp->dnalengthname || dp->dnalengthfixed)|| !dp->dnaname)) {
935                                 fprintf(stderr, "%s: %s.%s has no valid dna info.\n",
936                                         __func__, srna->identifier, prop->identifier);
937                                 DefRNA.error = 1;
938                                 return NULL;
939                         }
940                 }
941
942                 func = rna_alloc_function_name(srna->identifier, rna_safe_id(prop->identifier), "length");
943
944                 fprintf(f, "int %s(PointerRNA *ptr)\n", func);
945                 fprintf(f, "{\n");
946                 if (manualfunc) {
947                         fprintf(f, "    return %s(ptr);\n", manualfunc);
948                 }
949                 else {
950                         rna_print_data_get(f, dp);
951                         if (dp->dnalengthname)
952                                 fprintf(f, "    return (data->%s == NULL)? 0: data->%s;\n", dp->dnaname, dp->dnalengthname);
953                         else
954                                 fprintf(f, "    return (data->%s == NULL)? 0: %d;\n", dp->dnaname, dp->dnalengthfixed);
955                 }
956                 fprintf(f, "}\n\n");
957         }
958
959         return func;
960 }
961
962 static char *rna_def_property_begin_func(FILE *f, StructRNA *srna, PropertyRNA *prop, PropertyDefRNA *dp, const char *manualfunc)
963 {
964         char *func, *getfunc;
965
966         if (prop->flag & PROP_IDPROPERTY && manualfunc == NULL)
967                 return NULL;
968
969         if (!manualfunc) {
970                 if (!dp->dnastructname || !dp->dnaname) {
971                         fprintf(stderr, "%s: %s.%s has no valid dna info.\n",
972                                 __func__, srna->identifier, prop->identifier);
973                         DefRNA.error = 1;
974                         return NULL;
975                 }
976         }
977
978         func = rna_alloc_function_name(srna->identifier, rna_safe_id(prop->identifier), "begin");
979
980         fprintf(f, "void %s(CollectionPropertyIterator *iter, PointerRNA *ptr)\n", func);
981         fprintf(f, "{\n");
982
983         if (!manualfunc)
984                 rna_print_data_get(f, dp);
985
986         fprintf(f, "\n  memset(iter, 0, sizeof(*iter));\n");
987         fprintf(f, "    iter->parent= *ptr;\n");
988         fprintf(f, "    iter->prop= (PropertyRNA*)&rna_%s_%s;\n", srna->identifier, prop->identifier);
989
990         if (dp->dnalengthname || dp->dnalengthfixed) {
991                 if (manualfunc) {
992                         fprintf(f, "\n  %s(iter, ptr);\n", manualfunc);
993                 }
994                 else {
995                         if (dp->dnalengthname)
996                                 fprintf(f, "\n  rna_iterator_array_begin(iter, data->%s, sizeof(data->%s[0]), data->%s, 0, NULL);\n", dp->dnaname, dp->dnaname, dp->dnalengthname);
997                         else
998                                 fprintf(f, "\n  rna_iterator_array_begin(iter, data->%s, sizeof(data->%s[0]), %d, 0, NULL);\n", dp->dnaname, dp->dnaname, dp->dnalengthfixed);
999                 }
1000         }
1001         else {
1002                 if (manualfunc)
1003                         fprintf(f, "\n  %s(iter, ptr);\n", manualfunc);
1004                 else if (dp->dnapointerlevel == 0)
1005                         fprintf(f, "\n  rna_iterator_listbase_begin(iter, &data->%s, NULL);\n", dp->dnaname);
1006                 else
1007                         fprintf(f, "\n  rna_iterator_listbase_begin(iter, data->%s, NULL);\n", dp->dnaname);
1008         }
1009
1010         getfunc = rna_alloc_function_name(srna->identifier, rna_safe_id(prop->identifier), "get");
1011
1012         fprintf(f, "\n  if(iter->valid)\n");
1013         fprintf(f, "            iter->ptr= %s(iter);\n", getfunc);
1014
1015         fprintf(f, "}\n\n");
1016
1017
1018         return func;
1019 }
1020
1021 static char *rna_def_property_lookup_int_func(FILE *f, StructRNA *srna, PropertyRNA *prop, PropertyDefRNA *dp, const char *manualfunc, const char *nextfunc)
1022 {
1023         /* note on indices, this is for external functions and ignores skipped values.
1024          * so the the index can only be checked against the length when there is no 'skip' funcion. */
1025         char *func;
1026
1027         if (prop->flag & PROP_IDPROPERTY && manualfunc == NULL)
1028                 return NULL;
1029
1030         if (!manualfunc) {
1031                 if (!dp->dnastructname || !dp->dnaname)
1032                         return NULL;
1033
1034                 /* only supported in case of standard next functions */
1035                 if (strcmp(nextfunc, "rna_iterator_array_next") == 0);
1036                 else if (strcmp(nextfunc, "rna_iterator_listbase_next") == 0);
1037                 else return NULL;
1038         }
1039
1040         func = rna_alloc_function_name(srna->identifier, rna_safe_id(prop->identifier), "lookup_int");
1041
1042         fprintf(f, "int %s(PointerRNA *ptr, int index, PointerRNA *r_ptr)\n", func);
1043         fprintf(f, "{\n");
1044
1045         if (manualfunc) {
1046                 fprintf(f, "\n  return %s(ptr, index, r_ptr);\n", manualfunc);
1047                 fprintf(f, "}\n\n");
1048                 return func;
1049         }
1050
1051         fprintf(f, "    int found= 0;\n");
1052         fprintf(f, "    CollectionPropertyIterator iter;\n\n");
1053
1054         fprintf(f, "    %s_%s_begin(&iter, ptr);\n\n", srna->identifier, rna_safe_id(prop->identifier));
1055         fprintf(f, "    if(iter.valid){\n");
1056
1057         if (strcmp(nextfunc, "rna_iterator_array_next") == 0) {
1058                 fprintf(f, "            ArrayIterator *internal= iter.internal;\n");
1059                 fprintf(f, "            if(index < 0 || index >= internal->length) {\n");
1060                 fprintf(f, "#ifdef __GNUC__\n");
1061                 fprintf(f, "                    printf(\"Array iterator out of range: %%s (index %%d)\\n\", __func__, index);\n");
1062                 fprintf(f, "#else\n");
1063                 fprintf(f, "                    printf(\"Array iterator out of range: (index %%d)\\n\", index);\n");
1064                 fprintf(f, "#endif\n");
1065                 fprintf(f, "            }\n");
1066                 fprintf(f, "            else if(internal->skip) {\n");
1067                 fprintf(f, "                    while(index-- > 0 && iter.valid) {\n");
1068                 fprintf(f, "                            rna_iterator_array_next(&iter);\n");
1069                 fprintf(f, "                    }\n");
1070                 fprintf(f, "                    found= (index == -1 && iter.valid);\n");
1071                 fprintf(f, "            }\n");
1072                 fprintf(f, "            else {\n");
1073                 fprintf(f, "                    internal->ptr += internal->itemsize*index;\n");
1074                 fprintf(f, "                    found= 1;\n");
1075                 fprintf(f, "            }\n");
1076         }
1077         else if (strcmp(nextfunc, "rna_iterator_listbase_next") == 0) {
1078                 fprintf(f, "            ListBaseIterator *internal= iter.internal;\n");
1079                 fprintf(f, "            if(internal->skip) {\n");
1080                 fprintf(f, "                    while(index-- > 0 && iter.valid) {\n");
1081                 fprintf(f, "                            rna_iterator_listbase_next(&iter);\n");
1082                 fprintf(f, "                    }\n");
1083                 fprintf(f, "                    found= (index == -1 && iter.valid);\n");
1084                 fprintf(f, "            }\n");
1085                 fprintf(f, "            else {\n");
1086                 fprintf(f, "                    while(index-- > 0 && internal->link)\n");
1087                 fprintf(f, "                            internal->link= internal->link->next;\n");
1088                 fprintf(f, "                    found= (index == -1 && internal->link);\n");
1089                 fprintf(f, "            }\n");
1090         }
1091
1092         fprintf(f, "            if(found) *r_ptr = %s_%s_get(&iter);\n", srna->identifier, rna_safe_id(prop->identifier));
1093         fprintf(f, "    }\n\n");
1094         fprintf(f, "    %s_%s_end(&iter);\n\n", srna->identifier, rna_safe_id(prop->identifier));
1095
1096         fprintf(f, "    return found;\n");
1097
1098 #if 0
1099         rna_print_data_get(f, dp);
1100         item_type = (cprop->item_type)? (const char*)cprop->item_type: "UnknownType";
1101
1102         if (dp->dnalengthname || dp->dnalengthfixed) {
1103                 if (dp->dnalengthname)
1104                         fprintf(f, "\n  rna_array_lookup_int(ptr, &RNA_%s, data->%s, sizeof(data->%s[0]), data->%s, index);\n", item_type, dp->dnaname, dp->dnaname, dp->dnalengthname);
1105                 else
1106                         fprintf(f, "\n  rna_array_lookup_int(ptr, &RNA_%s, data->%s, sizeof(data->%s[0]), %d, index);\n", item_type, dp->dnaname, dp->dnaname, dp->dnalengthfixed);
1107         }
1108         else {
1109                 if (dp->dnapointerlevel == 0)
1110                         fprintf(f, "\n  return rna_listbase_lookup_int(ptr, &RNA_%s, &data->%s, index);\n", item_type, dp->dnaname);
1111                 else
1112                         fprintf(f, "\n  return rna_listbase_lookup_int(ptr, &RNA_%s, data->%s, index);\n", item_type, dp->dnaname);
1113         }
1114 #endif
1115
1116         fprintf(f, "}\n\n");
1117
1118         return func;
1119 }
1120
1121 static char *rna_def_property_next_func(FILE *f, StructRNA *srna, PropertyRNA *prop, PropertyDefRNA *dp, const char *manualfunc)
1122 {
1123         char *func, *getfunc;
1124
1125         if (prop->flag & PROP_IDPROPERTY && manualfunc == NULL)
1126                 return NULL;
1127
1128         if (!manualfunc)
1129                 return NULL;
1130
1131         func = rna_alloc_function_name(srna->identifier, rna_safe_id(prop->identifier), "next");
1132
1133         fprintf(f, "void %s(CollectionPropertyIterator *iter)\n", func);
1134         fprintf(f, "{\n");
1135         fprintf(f, "    %s(iter);\n", manualfunc);
1136
1137         getfunc = rna_alloc_function_name(srna->identifier, rna_safe_id(prop->identifier), "get");
1138
1139         fprintf(f, "\n  if(iter->valid)\n");
1140         fprintf(f, "            iter->ptr= %s(iter);\n", getfunc);
1141
1142         fprintf(f, "}\n\n");
1143
1144         return func;
1145 }
1146
1147 static char *rna_def_property_end_func(FILE *f, StructRNA *srna, PropertyRNA *prop, PropertyDefRNA *dp, const char *manualfunc)
1148 {
1149         char *func;
1150
1151         if (prop->flag & PROP_IDPROPERTY && manualfunc == NULL)
1152                 return NULL;
1153
1154         func = rna_alloc_function_name(srna->identifier, rna_safe_id(prop->identifier), "end");
1155
1156         fprintf(f, "void %s(CollectionPropertyIterator *iter)\n", func);
1157         fprintf(f, "{\n");
1158         if (manualfunc)
1159                 fprintf(f, "    %s(iter);\n", manualfunc);
1160         fprintf(f, "}\n\n");
1161
1162         return func;
1163 }
1164
1165 static void rna_set_raw_property(PropertyDefRNA *dp, PropertyRNA *prop)
1166 {
1167         if (dp->dnapointerlevel != 0)
1168                 return;
1169         if (!dp->dnatype || !dp->dnaname || !dp->dnastructname)
1170                 return;
1171         
1172         if (strcmp(dp->dnatype, "char") == 0) {
1173                 prop->rawtype = PROP_RAW_CHAR;
1174                 prop->flag |= PROP_RAW_ACCESS;
1175         }
1176         else if (strcmp(dp->dnatype, "short") == 0) {
1177                 prop->rawtype = PROP_RAW_SHORT;
1178                 prop->flag |= PROP_RAW_ACCESS;
1179         }
1180         else if (strcmp(dp->dnatype, "int") == 0) {
1181                 prop->rawtype = PROP_RAW_INT;
1182                 prop->flag |= PROP_RAW_ACCESS;
1183         }
1184         else if (strcmp(dp->dnatype, "float") == 0) {
1185                 prop->rawtype = PROP_RAW_FLOAT;
1186                 prop->flag |= PROP_RAW_ACCESS;
1187         }
1188         else if (strcmp(dp->dnatype, "double") == 0) {
1189                 prop->rawtype = PROP_RAW_DOUBLE;
1190                 prop->flag |= PROP_RAW_ACCESS;
1191         }
1192 }
1193
1194 static void rna_set_raw_offset(FILE *f, StructRNA *srna, PropertyRNA *prop)
1195 {
1196         PropertyDefRNA *dp = rna_find_struct_property_def(srna, prop);
1197
1198         fprintf(f, "\toffsetof(%s, %s), %d", dp->dnastructname, dp->dnaname, prop->rawtype);
1199 }
1200
1201 static void rna_def_property_funcs(FILE *f, StructRNA *srna, PropertyDefRNA *dp)
1202 {
1203         PropertyRNA *prop;
1204
1205         prop = dp->prop;
1206
1207         switch (prop->type) {
1208                 case PROP_BOOLEAN: {
1209                         BoolPropertyRNA *bprop = (BoolPropertyRNA*)prop;
1210
1211                         if (!prop->arraydimension) {
1212                                 if (!bprop->get && !bprop->set && !dp->booleanbit)
1213                                         rna_set_raw_property(dp, prop);
1214
1215                                 bprop->get = (void*)rna_def_property_get_func(f, srna, prop, dp, (const char*)bprop->get);
1216                                 bprop->set = (void*)rna_def_property_set_func(f, srna, prop, dp, (const char*)bprop->set);
1217                         }
1218                         else {
1219                                 bprop->getarray = (void*)rna_def_property_get_func(f, srna, prop, dp, (const char*)bprop->getarray);
1220                                 bprop->setarray = (void*)rna_def_property_set_func(f, srna, prop, dp, (const char*)bprop->setarray);
1221                         }
1222                         break;
1223                 }
1224                 case PROP_INT: {
1225                         IntPropertyRNA *iprop = (IntPropertyRNA*)prop;
1226
1227                         if (!prop->arraydimension) {
1228                                 if (!iprop->get && !iprop->set)
1229                                         rna_set_raw_property(dp, prop);
1230
1231                                 iprop->get = (void*)rna_def_property_get_func(f, srna, prop, dp, (const char*)iprop->get);
1232                                 iprop->set = (void*)rna_def_property_set_func(f, srna, prop, dp, (const char*)iprop->set);
1233                         }
1234                         else {
1235                                 if (!iprop->getarray && !iprop->setarray)
1236                                         rna_set_raw_property(dp, prop);
1237
1238                                 iprop->getarray = (void*)rna_def_property_get_func(f, srna, prop, dp, (const char*)iprop->getarray);
1239                                 iprop->setarray = (void*)rna_def_property_set_func(f, srna, prop, dp, (const char*)iprop->setarray);
1240                         }
1241                         break;
1242                 }
1243                 case PROP_FLOAT: {
1244                         FloatPropertyRNA *fprop = (FloatPropertyRNA*)prop;
1245
1246                         if (!prop->arraydimension) {
1247                                 if (!fprop->get && !fprop->set)
1248                                         rna_set_raw_property(dp, prop);
1249
1250                                 fprop->get = (void*)rna_def_property_get_func(f, srna, prop, dp, (const char*)fprop->get);
1251                                 fprop->set = (void*)rna_def_property_set_func(f, srna, prop, dp, (const char*)fprop->set);
1252                         }
1253                         else {
1254                                 if (!fprop->getarray && !fprop->setarray)
1255                                         rna_set_raw_property(dp, prop);
1256
1257                                 fprop->getarray = (void*)rna_def_property_get_func(f, srna, prop, dp, (const char*)fprop->getarray);
1258                                 fprop->setarray = (void*)rna_def_property_set_func(f, srna, prop, dp, (const char*)fprop->setarray);
1259                         }
1260                         break;
1261                 }
1262                 case PROP_ENUM: {
1263                         EnumPropertyRNA *eprop = (EnumPropertyRNA*)prop;
1264
1265                         eprop->get = (void*)rna_def_property_get_func(f, srna, prop, dp, (const char*)eprop->get);
1266                         eprop->set = (void*)rna_def_property_set_func(f, srna, prop, dp, (const char*)eprop->set);
1267                         break;
1268                 }
1269                 case PROP_STRING: {
1270                         StringPropertyRNA *sprop = (StringPropertyRNA*)prop;
1271
1272                         sprop->get = (void*)rna_def_property_get_func(f, srna, prop, dp, (const char*)sprop->get);
1273                         sprop->length = (void*)rna_def_property_length_func(f, srna, prop, dp, (const char*)sprop->length);
1274                         sprop->set = (void*)rna_def_property_set_func(f, srna, prop, dp, (const char*)sprop->set);
1275                         break;
1276                 }
1277                 case PROP_POINTER: {
1278                         PointerPropertyRNA *pprop = (PointerPropertyRNA*)prop;
1279
1280                         pprop->get = (void*)rna_def_property_get_func(f, srna, prop, dp, (const char*)pprop->get);
1281                         pprop->set = (void*)rna_def_property_set_func(f, srna, prop, dp, (const char*)pprop->set);
1282                         if (!pprop->type) {
1283                                 fprintf(stderr, "%s: %s.%s, pointer must have a struct type.\n",
1284                                         __func__, srna->identifier, prop->identifier);
1285                                 DefRNA.error = 1;
1286                         }
1287                         break;
1288                 }
1289                 case PROP_COLLECTION: {
1290                         CollectionPropertyRNA *cprop = (CollectionPropertyRNA*)prop;
1291                         const char *nextfunc = (const char*)cprop->next;
1292
1293                         if (dp->dnatype && strcmp(dp->dnatype, "ListBase") == 0);
1294                         else if (dp->dnalengthname || dp->dnalengthfixed)
1295                                 cprop->length = (void*)rna_def_property_length_func(f, srna, prop, dp, (const char*)cprop->length);
1296
1297                         /* test if we can allow raw array access, if it is using our standard
1298                          * array get/next function, we can be sure it is an actual array */
1299                         if (cprop->next && cprop->get)
1300                                 if (strcmp((const char*)cprop->next, "rna_iterator_array_next") == 0 &&
1301                                    strcmp((const char*)cprop->get, "rna_iterator_array_get") == 0)
1302                                         prop->flag |= PROP_RAW_ARRAY;
1303
1304                         cprop->get = (void*)rna_def_property_get_func(f, srna, prop, dp, (const char*)cprop->get);
1305                         cprop->begin = (void*)rna_def_property_begin_func(f, srna, prop, dp, (const char*)cprop->begin);
1306                         cprop->next = (void*)rna_def_property_next_func(f, srna, prop, dp, (const char*)cprop->next);
1307                         cprop->end = (void*)rna_def_property_end_func(f, srna, prop, dp, (const char*)cprop->end);
1308                         cprop->lookupint = (void*)rna_def_property_lookup_int_func(f, srna, prop, dp, (const char*)cprop->lookupint, nextfunc);
1309
1310                         if (!(prop->flag & PROP_IDPROPERTY)) {
1311                                 if (!cprop->begin) {
1312                                         fprintf(stderr, "%s: %s.%s, collection must have a begin function.\n",
1313                                                 __func__, srna->identifier, prop->identifier);
1314                                         DefRNA.error = 1;
1315                                 }
1316                                 if (!cprop->next) {
1317                                         fprintf(stderr, "%s: %s.%s, collection must have a next function.\n",
1318                                                 __func__, srna->identifier, prop->identifier);
1319                                         DefRNA.error = 1;
1320                                 }
1321                                 if (!cprop->get) {
1322                                         fprintf(stderr, "%s: %s.%s, collection must have a get function.\n",
1323                                                 __func__, srna->identifier, prop->identifier);
1324                                         DefRNA.error = 1;
1325                                 }
1326                         }
1327                         if (!cprop->item_type) {
1328                                 fprintf(stderr, "%s: %s.%s, collection must have a struct type.\n",
1329                                         __func__, srna->identifier, prop->identifier);
1330                                 DefRNA.error = 1;
1331                         }
1332                         break;
1333                 }
1334         }
1335 }
1336
1337 static void rna_def_property_funcs_header(FILE *f, StructRNA *srna, PropertyDefRNA *dp)
1338 {
1339         PropertyRNA *prop;
1340         char *func;
1341
1342         prop = dp->prop;
1343
1344         if (prop->flag & (PROP_IDPROPERTY|PROP_BUILTIN))
1345                 return;
1346
1347         func = rna_alloc_function_name(srna->identifier, rna_safe_id(prop->identifier), "");
1348
1349         switch (prop->type) {
1350                 case PROP_BOOLEAN:
1351                 case PROP_INT: {
1352                         if (!prop->arraydimension) {
1353                                 fprintf(f, "int %sget(PointerRNA *ptr);\n", func);
1354                                 /*fprintf(f, "void %sset(PointerRNA *ptr, int value);\n", func); */
1355                         }
1356                         else if (prop->arraydimension && prop->totarraylength) {
1357                                 fprintf(f, "void %sget(PointerRNA *ptr, int values[%u]);\n", func, prop->totarraylength);
1358                                 /*fprintf(f, "void %sset(PointerRNA *ptr, const int values[%d]);\n", func, prop->arraylength); */
1359                         }
1360                         else {
1361                                 fprintf(f, "void %sget(PointerRNA *ptr, int values[]);\n", func);
1362                                 /*fprintf(f, "void %sset(PointerRNA *ptr, const int values[]);\n", func); */
1363                         }
1364                         break;
1365                 }
1366                 case PROP_FLOAT: {
1367                         if (!prop->arraydimension) {
1368                                 fprintf(f, "float %sget(PointerRNA *ptr);\n", func);
1369                                 /*fprintf(f, "void %sset(PointerRNA *ptr, float value);\n", func); */
1370                         }
1371                         else if (prop->arraydimension && prop->totarraylength) {
1372                                 fprintf(f, "void %sget(PointerRNA *ptr, float values[%u]);\n", func, prop->totarraylength);
1373                                 /*fprintf(f, "void %sset(PointerRNA *ptr, const float values[%d]);\n", func, prop->arraylength); */
1374                         }
1375                         else {
1376                                 fprintf(f, "void %sget(PointerRNA *ptr, float values[]);\n", func);
1377                                 /*fprintf(f, "void %sset(PointerRNA *ptr, const float values[]);\n", func); */
1378                         }
1379                         break;
1380                 }
1381                 case PROP_ENUM: {
1382                         EnumPropertyRNA *eprop = (EnumPropertyRNA*)prop;
1383                         int i;
1384
1385                         if (eprop->item) {
1386                                 fprintf(f, "enum {\n");
1387
1388                                 for (i = 0; i<eprop->totitem; i++)
1389                                         if (eprop->item[i].identifier[0])
1390                                                 fprintf(f, "\t%s_%s_%s = %d,\n", srna->identifier, prop->identifier, eprop->item[i].identifier, eprop->item[i].value);
1391
1392                                 fprintf(f, "};\n\n");
1393                         }
1394
1395                         fprintf(f, "int %sget(PointerRNA *ptr);\n", func);
1396                         /*fprintf(f, "void %sset(PointerRNA *ptr, int value);\n", func); */
1397
1398                         break;
1399                 }
1400                 case PROP_STRING: {
1401                         StringPropertyRNA *sprop = (StringPropertyRNA*)prop;
1402
1403                         if (sprop->maxlength) {
1404                                 fprintf(f, "#define %s_%s_MAX %d\n\n", srna->identifier, prop->identifier, sprop->maxlength);
1405                         }
1406                         
1407                         fprintf(f, "void %sget(PointerRNA *ptr, char *value);\n", func);
1408                         fprintf(f, "int %slength(PointerRNA *ptr);\n", func);
1409                         /*fprintf(f, "void %sset(PointerRNA *ptr, const char *value);\n", func); */
1410
1411                         break;
1412                 }
1413                 case PROP_POINTER: {
1414                         fprintf(f, "PointerRNA %sget(PointerRNA *ptr);\n", func);
1415                         /*fprintf(f, "void %sset(PointerRNA *ptr, PointerRNA value);\n", func); */
1416                         break;
1417                 }
1418                 case PROP_COLLECTION: {
1419                         fprintf(f, "void %sbegin(CollectionPropertyIterator *iter, PointerRNA *ptr);\n", func);
1420                         fprintf(f, "void %snext(CollectionPropertyIterator *iter);\n", func);
1421                         fprintf(f, "void %send(CollectionPropertyIterator *iter);\n", func);
1422                         /*fprintf(f, "int %slength(PointerRNA *ptr);\n", func); */
1423                         /*fprintf(f, "void %slookup_int(PointerRNA *ptr, int key, StructRNA **type);\n", func); */
1424                         /*fprintf(f, "void %slookup_string(PointerRNA *ptr, const char *key, StructRNA **type);\n", func); */
1425                         break;
1426                 }
1427         }
1428
1429         fprintf(f, "\n");
1430 }
1431
1432 static void rna_def_property_funcs_header_cpp(FILE *f, StructRNA *srna, PropertyDefRNA *dp)
1433 {
1434         PropertyRNA *prop;
1435
1436         prop = dp->prop;
1437
1438         if (prop->flag & (PROP_IDPROPERTY|PROP_BUILTIN))
1439                 return;
1440         
1441         if (prop->name && prop->description && prop->description[0] != '\0')
1442                 fprintf(f, "\t/* %s: %s */\n", prop->name, prop->description);
1443         else if (prop->name)
1444                 fprintf(f, "\t/* %s */\n", prop->name);
1445         else
1446                 fprintf(f, "\t/* */\n");
1447
1448         switch (prop->type) {
1449                 case PROP_BOOLEAN: {
1450                         if (!prop->arraydimension)
1451                                 fprintf(f, "\tinline bool %s(void);", rna_safe_id(prop->identifier));
1452                         else if (prop->totarraylength)
1453                                 fprintf(f, "\tinline Array<int, %u> %s(void);", prop->totarraylength, rna_safe_id(prop->identifier));
1454                         break;
1455                 }
1456                 case PROP_INT: {
1457                         if (!prop->arraydimension)
1458                                 fprintf(f, "\tinline int %s(void);", rna_safe_id(prop->identifier));
1459                         else if (prop->totarraylength)
1460                                 fprintf(f, "\tinline Array<int, %u> %s(void);", prop->totarraylength, rna_safe_id(prop->identifier));
1461                         break;
1462                 }
1463                 case PROP_FLOAT: {
1464                         if (!prop->arraydimension)
1465                                 fprintf(f, "\tinline float %s(void);", rna_safe_id(prop->identifier));
1466                         else if (prop->totarraylength)
1467                                 fprintf(f, "\tinline Array<float, %u> %s(void);", prop->totarraylength, rna_safe_id(prop->identifier));
1468                         break;
1469                 }
1470                 case PROP_ENUM: {
1471                         EnumPropertyRNA *eprop = (EnumPropertyRNA*)prop;
1472                         int i;
1473
1474                         if (eprop->item) {
1475                                 fprintf(f, "\tenum %s_enum {\n", rna_safe_id(prop->identifier));
1476
1477                                 for (i = 0; i<eprop->totitem; i++)
1478                                         if (eprop->item[i].identifier[0])
1479                                                 fprintf(f, "\t\t%s_%s = %d,\n", rna_safe_id(prop->identifier), eprop->item[i].identifier, eprop->item[i].value);
1480
1481                                 fprintf(f, "\t};\n");
1482                         }
1483
1484                         fprintf(f, "\tinline %s_enum %s(void);", rna_safe_id(prop->identifier), rna_safe_id(prop->identifier));
1485                         break;
1486                 }
1487                 case PROP_STRING: {
1488                         fprintf(f, "\tinline std::string %s(void);", rna_safe_id(prop->identifier));
1489                         break;
1490                 }
1491                 case PROP_POINTER: {
1492                         PointerPropertyRNA *pprop = (PointerPropertyRNA*)dp->prop;
1493
1494                         if (pprop->type)
1495                                 fprintf(f, "\tinline %s %s(void);", (const char*)pprop->type, rna_safe_id(prop->identifier));
1496                         else
1497                                 fprintf(f, "\tinline %s %s(void);", "UnknownType", rna_safe_id(prop->identifier));
1498                         break;
1499                 }
1500                 case PROP_COLLECTION: {
1501                         CollectionPropertyRNA *cprop = (CollectionPropertyRNA*)dp->prop;
1502
1503                         if (cprop->item_type)
1504                                 fprintf(f, "\tCOLLECTION_PROPERTY(%s, %s, %s)", (const char*)cprop->item_type, srna->identifier, rna_safe_id(prop->identifier));
1505                         else
1506                                 fprintf(f, "\tCOLLECTION_PROPERTY(%s, %s, %s)", "UnknownType", srna->identifier, rna_safe_id(prop->identifier));
1507                         break;
1508                 }
1509         }
1510
1511         fprintf(f, "\n");
1512 }
1513
1514 static void rna_def_property_funcs_impl_cpp(FILE *f, StructRNA *srna, PropertyDefRNA *dp)
1515 {
1516         PropertyRNA *prop;
1517
1518         prop = dp->prop;
1519
1520         if (prop->flag & (PROP_IDPROPERTY|PROP_BUILTIN))
1521                 return;
1522
1523         switch (prop->type) {
1524                 case PROP_BOOLEAN: {
1525                         if (!prop->arraydimension)
1526                                 fprintf(f, "\tBOOLEAN_PROPERTY(%s, %s)", srna->identifier, rna_safe_id(prop->identifier));
1527                         else if (prop->totarraylength)
1528                                 fprintf(f, "\tBOOLEAN_ARRAY_PROPERTY(%s, %u, %s)", srna->identifier, prop->totarraylength, rna_safe_id(prop->identifier));
1529                         break;
1530                 }
1531                 case PROP_INT: {
1532                         if (!prop->arraydimension)
1533                                 fprintf(f, "\tINT_PROPERTY(%s, %s)", srna->identifier, rna_safe_id(prop->identifier));
1534                         else if (prop->totarraylength)
1535                                 fprintf(f, "\tINT_ARRAY_PROPERTY(%s, %u, %s)", srna->identifier, prop->totarraylength, rna_safe_id(prop->identifier));
1536                         break;
1537                 }
1538                 case PROP_FLOAT: {
1539                         if (!prop->arraydimension)
1540                                 fprintf(f, "\tFLOAT_PROPERTY(%s, %s)", srna->identifier, rna_safe_id(prop->identifier));
1541                         else if (prop->totarraylength)
1542                                 fprintf(f, "\tFLOAT_ARRAY_PROPERTY(%s, %u, %s)", srna->identifier, prop->totarraylength, rna_safe_id(prop->identifier));
1543                         break;
1544                 }
1545                 case PROP_ENUM: {
1546                         fprintf(f, "\tENUM_PROPERTY(%s_enum, %s, %s)", rna_safe_id(prop->identifier), srna->identifier, rna_safe_id(prop->identifier));
1547
1548                         break;
1549                 }
1550                 case PROP_STRING: {
1551                         fprintf(f, "\tSTRING_PROPERTY(%s, %s)", srna->identifier, rna_safe_id(prop->identifier));
1552                         break;
1553                 }
1554                 case PROP_POINTER: {
1555                         PointerPropertyRNA *pprop = (PointerPropertyRNA*)dp->prop;
1556
1557                         if (pprop->type)
1558                                 fprintf(f, "\tPOINTER_PROPERTY(%s, %s, %s)", (const char*)pprop->type, srna->identifier, rna_safe_id(prop->identifier));
1559                         else
1560                                 fprintf(f, "\tPOINTER_PROPERTY(%s, %s, %s)", "UnknownType", srna->identifier, rna_safe_id(prop->identifier));
1561                         break;
1562                 }
1563                 case PROP_COLLECTION: {
1564                         /*CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)dp->prop;
1565
1566                         if(cprop->type)
1567                                 fprintf(f, "\tCOLLECTION_PROPERTY(%s, %s, %s)", (const char*)cprop->type, srna->identifier, prop->identifier);
1568                         else
1569                                 fprintf(f, "\tCOLLECTION_PROPERTY(%s, %s, %s)", "UnknownType", srna->identifier, prop->identifier);*/
1570                         break;
1571                 }
1572         }
1573
1574         fprintf(f, "\n");
1575 }
1576
1577 static void rna_def_function_funcs(FILE *f, StructDefRNA *dsrna, FunctionDefRNA *dfunc)
1578 {
1579         StructRNA *srna;
1580         FunctionRNA *func;
1581         PropertyDefRNA *dparm;
1582         PropertyType type;
1583         const char *funcname, *valstr;
1584         const char *ptrstr;
1585         const short has_data = (dfunc->cont.properties.first != NULL);
1586         int flag, pout, cptr, first;
1587
1588         srna = dsrna->srna;
1589         func = dfunc->func;
1590
1591         if (!dfunc->call)
1592                 return;
1593
1594         funcname = rna_alloc_function_name(srna->identifier, func->identifier, "call");
1595
1596         /* function definition */
1597         fprintf(f, "void %s(bContext *C, ReportList *reports, PointerRNA *_ptr, ParameterList *_parms)", funcname);
1598         fprintf(f, "\n{\n");
1599
1600         /* variable definitions */
1601         
1602         if (func->flag & FUNC_USE_SELF_ID) {
1603                 fprintf(f, "\tstruct ID *_selfid;\n");
1604         }
1605
1606         if ((func->flag & FUNC_NO_SELF) == 0) {
1607                 if (dsrna->dnaname) fprintf(f, "\tstruct %s *_self;\n", dsrna->dnaname);
1608                 else fprintf(f, "\tstruct %s *_self;\n", srna->identifier);
1609         }
1610
1611         dparm = dfunc->cont.properties.first;
1612         for (; dparm; dparm = dparm->next) {
1613                 type = dparm->prop->type;
1614                 flag = dparm->prop->flag;
1615                 pout = (flag & PROP_OUTPUT);
1616                 cptr = ((type == PROP_POINTER) && !(flag & PROP_RNAPTR));
1617
1618                 if (dparm->prop == func->c_ret)
1619                         ptrstr = cptr || dparm->prop->arraydimension ? "*" : "";
1620                 /* XXX only arrays and strings are allowed to be dynamic, is this checked anywhere? */
1621                 else if (cptr || (flag & PROP_DYNAMIC))
1622                         ptrstr = pout ? "**" : "*";
1623                 /* fixed size arrays and RNA pointers are pre-allocated on the ParameterList stack, pass a pointer to it */
1624                 else if (type == PROP_POINTER || dparm->prop->arraydimension)
1625                         ptrstr = "*";
1626                 /* PROP_THICK_WRAP strings are pre-allocated on the ParameterList stack, but type name for string props is already char*, so leave empty */
1627                 else if (type == PROP_STRING && (flag & PROP_THICK_WRAP))
1628                         ptrstr = "";
1629                 else
1630                         ptrstr = pout ? "*" : "";
1631
1632                 /* for dynamic parameters we pass an additional int for the length of the parameter */
1633                 if (flag & PROP_DYNAMIC)
1634                         fprintf(f, "\tint %s%s_len;\n", pout ? "*" : "", dparm->prop->identifier);
1635                 
1636                 fprintf(f, "\t%s%s %s%s;\n", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop), ptrstr, dparm->prop->identifier);
1637         }
1638
1639         if (has_data) {
1640                 fprintf(f, "\tchar *_data");
1641                 if (func->c_ret) fprintf(f, ", *_retdata");
1642                 fprintf(f, ";\n");
1643                 fprintf(f, "\t\n");
1644         }
1645
1646         /* assign self */
1647         if (func->flag & FUNC_USE_SELF_ID) {
1648                 fprintf(f, "\t_selfid= (struct ID*)_ptr->id.data;\n");
1649         }
1650         
1651         if ((func->flag & FUNC_NO_SELF) == 0) {
1652                 if (dsrna->dnaname) fprintf(f, "\t_self= (struct %s *)_ptr->data;\n", dsrna->dnaname);
1653                 else fprintf(f, "\t_self= (struct %s *)_ptr->data;\n", srna->identifier);
1654         }
1655
1656         if (has_data) {
1657                 fprintf(f, "\t_data= (char *)_parms->data;\n");
1658         }
1659
1660         dparm = dfunc->cont.properties.first;
1661         for (; dparm; dparm = dparm->next) {
1662                 type = dparm->prop->type;
1663                 flag = dparm->prop->flag;
1664                 pout = (flag & PROP_OUTPUT);
1665                 cptr = ((type == PROP_POINTER) && !(flag & PROP_RNAPTR));
1666
1667                 if (dparm->prop == func->c_ret)
1668                         fprintf(f, "\t_retdata= _data;\n");
1669                 else  {
1670                         const char *data_str;
1671                         if (cptr || (flag & PROP_DYNAMIC)) {
1672                                 ptrstr = "**";
1673                                 valstr = "*";
1674                         }
1675                         else if (type == PROP_POINTER || dparm->prop->arraydimension) {
1676                                 ptrstr = "*";
1677                                 valstr = "";
1678                         }
1679                         else if (type == PROP_STRING && (flag & PROP_THICK_WRAP)) {
1680                                 ptrstr = "";
1681                                 valstr = "";
1682                         }
1683                         else {
1684                                 ptrstr = "*";
1685                                 valstr = "*";
1686                         }
1687
1688                         /* this must be kept in sync with RNA_parameter_length_get_data, we could just call the function directly, but this is faster */
1689                         if (flag & PROP_DYNAMIC) {
1690                                 fprintf(f, "\t%s_len= %s((int *)_data);\n", dparm->prop->identifier, pout ? "" : "*");
1691                                 data_str = "(&(((char *)_data)[sizeof(void *)]))";
1692                         }
1693                         else {
1694                                 data_str = "_data";
1695                         }
1696                         fprintf(f, "\t%s= ", dparm->prop->identifier);
1697
1698                         if (!pout)
1699                                 fprintf(f, "%s", valstr);
1700
1701                         fprintf(f, "((%s%s%s)%s);\n", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop), ptrstr, data_str);
1702                 }
1703
1704                 if (dparm->next)
1705                         fprintf(f, "\t_data+= %d;\n", rna_parameter_size_alloc(dparm->prop));
1706         }
1707
1708         if (dfunc->call) {
1709                 fprintf(f, "\t\n");
1710                 fprintf(f, "\t");
1711                 if (func->c_ret) fprintf(f, "%s= ", func->c_ret->identifier);
1712                 fprintf(f, "%s(", dfunc->call);
1713
1714                 first = 1;
1715
1716                 if (func->flag & FUNC_USE_SELF_ID) {
1717                         fprintf(f, "_selfid");
1718                         first = 0;
1719                 }
1720
1721                 if ((func->flag & FUNC_NO_SELF) == 0) {
1722                         if (!first) fprintf(f, ", ");
1723                         fprintf(f, "_self");
1724                         first = 0;
1725                 }
1726
1727                 if (func->flag & FUNC_USE_MAIN) {
1728                         if (!first) fprintf(f, ", ");
1729                         first = 0;
1730                         fprintf(f, "CTX_data_main(C)"); /* may have direct access later */
1731                 }
1732
1733                 if (func->flag & FUNC_USE_CONTEXT) {
1734                         if (!first) fprintf(f, ", ");
1735                         first = 0;
1736                         fprintf(f, "C");
1737                 }
1738
1739                 if (func->flag & FUNC_USE_REPORTS) {
1740                         if (!first) fprintf(f, ", ");
1741                         first = 0;
1742                         fprintf(f, "reports");
1743                 }
1744
1745                 dparm = dfunc->cont.properties.first;
1746                 for (; dparm; dparm = dparm->next) {
1747                         if (dparm->prop == func->c_ret)
1748                                 continue;
1749
1750                         if (!first) fprintf(f, ", ");
1751                         first = 0;
1752
1753                         if (dparm->prop->flag & PROP_DYNAMIC)
1754                                 fprintf(f, "%s_len, %s", dparm->prop->identifier, dparm->prop->identifier);
1755                         else
1756                                 fprintf(f, "%s", dparm->prop->identifier);
1757                 }
1758
1759                 fprintf(f, ");\n");
1760
1761                 if (func->c_ret) {
1762                         dparm = rna_find_parameter_def(func->c_ret);
1763                         ptrstr = (((dparm->prop->type == PROP_POINTER) && !(dparm->prop->flag & PROP_RNAPTR)) || (dparm->prop->arraydimension))? "*": "";
1764                         fprintf(f, "\t*((%s%s%s*)_retdata)= %s;\n", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop), ptrstr, func->c_ret->identifier);
1765                 }
1766         }
1767
1768         fprintf(f, "}\n\n");
1769
1770         dfunc->gencall = funcname;
1771 }
1772
1773 static void rna_auto_types(void)
1774 {
1775         StructDefRNA *ds;
1776         PropertyDefRNA *dp;
1777
1778         for (ds = DefRNA.structs.first; ds; ds = ds->cont.next) {
1779                 /* DNA name for Screen is patched in 2.5, we do the reverse here .. */
1780                 if (ds->dnaname && strcmp(ds->dnaname, "Screen") == 0)
1781                         ds->dnaname = "bScreen";
1782
1783                 for (dp = ds->cont.properties.first; dp; dp = dp->next) {
1784                         if (dp->dnastructname && strcmp(dp->dnastructname, "Screen") == 0)
1785                                 dp->dnastructname = "bScreen";
1786
1787                         if (dp->dnatype) {
1788                                 if (dp->prop->type == PROP_POINTER) {
1789                                         PointerPropertyRNA *pprop = (PointerPropertyRNA*)dp->prop;
1790                                         StructRNA *type;
1791
1792                                         if (!pprop->type && !pprop->get)
1793                                                 pprop->type = (StructRNA*)rna_find_type(dp->dnatype);
1794
1795                                         if (pprop->type) {
1796                                                 type = rna_find_struct((const char*)pprop->type);
1797                                                 if (type && (type->flag & STRUCT_ID_REFCOUNT))
1798                                                         pprop->property.flag |= PROP_ID_REFCOUNT;
1799                                         }
1800                                 }
1801                                 else if (dp->prop->type == PROP_COLLECTION) {
1802                                         CollectionPropertyRNA *cprop = (CollectionPropertyRNA*)dp->prop;
1803
1804                                         if (!cprop->item_type && !cprop->get && strcmp(dp->dnatype, "ListBase") == 0)
1805                                                 cprop->item_type = (StructRNA*)rna_find_type(dp->dnatype);
1806                                 }
1807                         }
1808                 }
1809         }
1810 }
1811
1812 static void rna_sort(BlenderRNA *brna)
1813 {
1814         StructDefRNA *ds;
1815         StructRNA *srna;
1816
1817         rna_sortlist(&brna->structs, cmp_struct);
1818         rna_sortlist(&DefRNA.structs, cmp_def_struct);
1819
1820         for (srna = brna->structs.first; srna; srna = srna->cont.next)
1821                 rna_sortlist(&srna->cont.properties, cmp_property);
1822
1823         for (ds = DefRNA.structs.first; ds; ds = ds->cont.next)
1824                 rna_sortlist(&ds->cont.properties, cmp_def_property);
1825 }
1826
1827 static const char *rna_property_structname(PropertyType type)
1828 {
1829         switch (type) {
1830                 case PROP_BOOLEAN: return "BoolPropertyRNA";
1831                 case PROP_INT: return "IntPropertyRNA";
1832                 case PROP_FLOAT: return "FloatPropertyRNA";
1833                 case PROP_STRING: return "StringPropertyRNA";
1834                 case PROP_ENUM: return "EnumPropertyRNA";
1835                 case PROP_POINTER: return "PointerPropertyRNA";
1836                 case PROP_COLLECTION: return "CollectionPropertyRNA";
1837                 default: return "UnknownPropertyRNA";
1838         }
1839 }
1840
1841 static const char *rna_property_subtypename(PropertySubType type)
1842 {
1843         switch (type) {
1844                 case PROP_NONE: return "PROP_NONE";
1845                 case PROP_FILEPATH: return "PROP_FILEPATH";
1846                 case PROP_FILENAME: return "PROP_FILENAME";
1847                 case PROP_DIRPATH: return "PROP_DIRPATH";
1848                 case PROP_BYTESTRING: return "PROP_BYTESTRING";
1849                 case PROP_TRANSLATE: return "PROP_TRANSLATE";
1850                 case PROP_UNSIGNED: return "PROP_UNSIGNED";
1851                 case PROP_PERCENTAGE: return "PROP_PERCENTAGE";
1852                 case PROP_FACTOR: return "PROP_FACTOR";
1853                 case PROP_ANGLE: return "PROP_ANGLE";
1854                 case PROP_TIME: return "PROP_TIME";
1855                 case PROP_DISTANCE: return "PROP_DISTANCE";
1856                 case PROP_COLOR: return "PROP_COLOR";
1857                 case PROP_TRANSLATION: return "PROP_TRANSLATION";
1858                 case PROP_DIRECTION: return "PROP_DIRECTION";
1859                 case PROP_MATRIX: return "PROP_MATRIX";
1860                 case PROP_EULER: return "PROP_EULER";
1861                 case PROP_QUATERNION: return "PROP_QUATERNION";
1862                 case PROP_AXISANGLE: return "PROP_AXISANGLE";
1863                 case PROP_VELOCITY: return "PROP_VELOCITY";
1864                 case PROP_ACCELERATION: return "PROP_ACCELERATION";
1865                 case PROP_XYZ: return "PROP_XYZ";
1866                 case PROP_COLOR_GAMMA: return "PROP_COLOR_GAMMA";
1867                 case PROP_COORDS: return "PROP_COORDS";
1868                 case PROP_LAYER: return "PROP_LAYER";
1869                 case PROP_LAYER_MEMBER: return "PROP_LAYER_MEMBER";
1870                 default: {
1871                         /* incase we dont have a type preset that includes the subtype */
1872                         if (RNA_SUBTYPE_UNIT(type)) {
1873                                 return rna_property_subtypename(type & ~RNA_SUBTYPE_UNIT(type));
1874                         }
1875                         else {
1876                                 return "PROP_SUBTYPE_UNKNOWN";
1877                         }
1878                 }
1879         }
1880 }
1881
1882 static const char *rna_property_subtype_unit(PropertySubType type)
1883 {
1884         switch (RNA_SUBTYPE_UNIT(type)) {
1885                 case PROP_UNIT_NONE:            return "PROP_UNIT_NONE";
1886                 case PROP_UNIT_LENGTH:          return "PROP_UNIT_LENGTH";
1887                 case PROP_UNIT_AREA:            return "PROP_UNIT_AREA";
1888                 case PROP_UNIT_VOLUME:          return "PROP_UNIT_VOLUME";
1889                 case PROP_UNIT_MASS:            return "PROP_UNIT_MASS";
1890                 case PROP_UNIT_ROTATION:        return "PROP_UNIT_ROTATION";
1891                 case PROP_UNIT_TIME:            return "PROP_UNIT_TIME";
1892                 case PROP_UNIT_VELOCITY:        return "PROP_UNIT_VELOCITY";
1893                 case PROP_UNIT_ACCELERATION:return "PROP_UNIT_ACCELERATION";
1894                 default:                                        return "PROP_UNIT_UNKNOWN";
1895         }
1896 }
1897
1898 static void rna_generate_prototypes(BlenderRNA *brna, FILE *f)
1899 {
1900         StructRNA *srna;
1901
1902         for (srna = brna->structs.first; srna; srna = srna->cont.next)
1903                 fprintf(f, "extern StructRNA RNA_%s;\n", srna->identifier);
1904         fprintf(f, "\n");
1905 }
1906
1907 static void rna_generate_blender(BlenderRNA *brna, FILE *f)
1908 {
1909         StructRNA *srna;
1910
1911         fprintf(f, "BlenderRNA BLENDER_RNA = {");
1912
1913         srna = brna->structs.first;
1914         if (srna) fprintf(f, "{&RNA_%s, ", srna->identifier);
1915         else fprintf(f, "{NULL, ");
1916
1917         srna = brna->structs.last;
1918         if (srna) fprintf(f, "&RNA_%s}", srna->identifier);
1919         else fprintf(f, "NULL}");
1920
1921         fprintf(f, "};\n\n");
1922 }
1923
1924 static void rna_generate_property_prototypes(BlenderRNA *brna, StructRNA *srna, FILE *f)
1925 {
1926         PropertyRNA *prop;
1927         StructRNA *base;
1928
1929         base = srna->base;
1930         while (base) {
1931                 fprintf(f, "\n");
1932                 for (prop = base->cont.properties.first; prop; prop = prop->next)
1933                         fprintf(f, "%s%s rna_%s_%s;\n", "extern ", rna_property_structname(prop->type), base->identifier, prop->identifier);
1934                 base = base->base;
1935         }
1936
1937         if (srna->cont.properties.first)
1938                 fprintf(f, "\n");
1939
1940         for (prop = srna->cont.properties.first; prop; prop = prop->next)
1941                 fprintf(f, "%s%s rna_%s_%s;\n", (prop->flag & PROP_EXPORT)? "": "", rna_property_structname(prop->type), srna->identifier, prop->identifier);
1942         fprintf(f, "\n");
1943 }
1944
1945 static void rna_generate_parameter_prototypes(BlenderRNA *brna, StructRNA *srna, FunctionRNA *func, FILE *f)
1946 {
1947         PropertyRNA *parm;
1948
1949         for (parm = func->cont.properties.first; parm; parm = parm->next)
1950                 fprintf(f, "%s%s rna_%s_%s_%s;\n", "extern ", rna_property_structname(parm->type), srna->identifier, func->identifier, parm->identifier);
1951
1952         if (func->cont.properties.first)
1953                 fprintf(f, "\n");
1954 }
1955
1956 static void rna_generate_function_prototypes(BlenderRNA *brna, StructRNA *srna, FILE *f)
1957 {
1958         FunctionRNA *func;
1959         StructRNA *base;
1960
1961         base = srna->base;
1962         while (base) {
1963                 for (func = base->functions.first; func; func = func->cont.next) {
1964                         fprintf(f, "%s%s rna_%s_%s_func;\n", "extern ", "FunctionRNA", base->identifier, func->identifier);
1965                         rna_generate_parameter_prototypes(brna, base, func, f);
1966                 }
1967
1968                 if (base->functions.first)
1969                         fprintf(f, "\n");
1970
1971                 base = base->base;
1972         }
1973
1974         for (func = srna->functions.first; func; func = func->cont.next) {
1975                 fprintf(f, "%s%s rna_%s_%s_func;\n", "extern ", "FunctionRNA", srna->identifier, func->identifier);
1976                 rna_generate_parameter_prototypes(brna, srna, func, f);
1977         }
1978
1979         if (srna->functions.first)
1980                 fprintf(f, "\n");
1981 }
1982
1983 static void rna_generate_static_parameter_prototypes(BlenderRNA *brna, StructRNA *srna, FunctionDefRNA *dfunc, FILE *f)
1984 {
1985         FunctionRNA *func;
1986         PropertyDefRNA *dparm;
1987         StructDefRNA *dsrna;
1988         PropertyType type;
1989         int flag, pout, cptr, first;
1990         const char *ptrstr;
1991
1992         dsrna = rna_find_struct_def(srna);
1993         func = dfunc->func;
1994
1995         /* return type */
1996         for (dparm = dfunc->cont.properties.first; dparm; dparm = dparm->next) {
1997                 if (dparm->prop == func->c_ret) {
1998                         if (dparm->prop->arraydimension)
1999                                 fprintf(f, "XXX no array return types yet"); /* XXX not supported */
2000                         else if (dparm->prop->type == PROP_POINTER && !(dparm->prop->flag & PROP_RNAPTR))
2001                                 fprintf(f, "%s%s *", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop));
2002                         else
2003                                 fprintf(f, "%s%s ", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop));
2004
2005                         break;
2006                 }
2007         }
2008
2009         /* void if nothing to return */
2010         if (!dparm)
2011                 fprintf(f, "void ");
2012
2013         /* function name */
2014         fprintf(f, "%s(", dfunc->call);
2015
2016         first = 1;
2017
2018         /* self, context and reports parameters */
2019         if (func->flag & FUNC_USE_SELF_ID) {
2020                 fprintf(f, "struct ID *_selfid");
2021                 first = 0;              
2022         }
2023         
2024         if ((func->flag & FUNC_NO_SELF) == 0) {
2025                 if (!first) fprintf(f, ", ");
2026                 if (dsrna->dnaname) fprintf(f, "struct %s *_self", dsrna->dnaname);
2027                 else fprintf(f, "struct %s *_self", srna->identifier);
2028                 first = 0;
2029         }
2030
2031         if (func->flag & FUNC_USE_MAIN) {
2032                 if (!first) fprintf(f, ", ");
2033                 first = 0;
2034                 fprintf(f, "Main *bmain");
2035         }
2036
2037         if (func->flag & FUNC_USE_CONTEXT) {
2038                 if (!first) fprintf(f, ", ");
2039                 first = 0;
2040                 fprintf(f, "bContext *C");
2041         }
2042
2043         if (func->flag & FUNC_USE_REPORTS) {
2044                 if (!first) fprintf(f, ", ");
2045                 first = 0;
2046                 fprintf(f, "ReportList *reports");
2047         }
2048
2049         /* defined parameters */
2050         for (dparm = dfunc->cont.properties.first; dparm; dparm = dparm->next) {
2051                 type = dparm->prop->type;
2052                 flag = dparm->prop->flag;
2053                 pout = (flag & PROP_OUTPUT);
2054                 cptr = ((type == PROP_POINTER) && !(flag & PROP_RNAPTR));
2055
2056                 if (dparm->prop == func->c_ret)
2057                         continue;
2058
2059                 if (cptr || (flag & PROP_DYNAMIC))
2060                         ptrstr = pout ? "**" : "*";
2061                 else if (type == PROP_POINTER || dparm->prop->arraydimension)
2062                         ptrstr = "*";
2063                 else if (type == PROP_STRING && (flag & PROP_THICK_WRAP))
2064                         ptrstr = "";
2065                 else
2066                         ptrstr = pout ? "*" : "";
2067
2068                 if (!first) fprintf(f, ", ");
2069                 first = 0;
2070
2071                 if (flag & PROP_DYNAMIC)
2072                         fprintf(f, "int %s%s_len, ", pout ? "*" : "", dparm->prop->identifier);
2073
2074                 if (!(flag & PROP_DYNAMIC) && dparm->prop->arraydimension)
2075                         fprintf(f, "%s%s %s[%u]", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop), dparm->prop->identifier, dparm->prop->totarraylength);
2076                 else
2077                         fprintf(f, "%s%s %s%s", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop), ptrstr, dparm->prop->identifier);
2078
2079         }
2080
2081         fprintf(f, ");\n");
2082 }
2083
2084 static void rna_generate_static_function_prototypes(BlenderRNA *brna, StructRNA *srna, FILE *f)
2085 {
2086         FunctionRNA *func;
2087         FunctionDefRNA *dfunc;
2088         int first = 1;
2089
2090         for (func = srna->functions.first; func; func = func->cont.next) {
2091                 dfunc = rna_find_function_def(func);
2092
2093                 if (dfunc->call) {
2094                         if (first) {
2095                                 fprintf(f, "/* Repeated prototypes to detect errors */\n\n");
2096                                 first = 0;
2097                         }
2098
2099                         rna_generate_static_parameter_prototypes(brna, srna, dfunc, f);
2100                 }
2101         }
2102
2103         fprintf(f, "\n");
2104 }
2105
2106 static void rna_generate_property(FILE *f, StructRNA *srna, const char *nest, PropertyRNA *prop) 
2107 {
2108         char *strnest = "", *errnest = "";
2109         int len, freenest = 0;
2110         
2111         if (nest != NULL) {
2112                 len = strlen(nest);
2113
2114                 strnest = MEM_mallocN(sizeof(char)*(len+2), "rna_generate_property -> strnest");
2115                 errnest = MEM_mallocN(sizeof(char)*(len+2), "rna_generate_property -> errnest");
2116
2117                 strcpy(strnest, "_"); strcat(strnest, nest);
2118                 strcpy(errnest, "."); strcat(errnest, nest);
2119
2120                 freenest = 1;
2121         }
2122
2123         switch (prop->type) {
2124                         case PROP_ENUM: {
2125                                 EnumPropertyRNA *eprop = (EnumPropertyRNA*)prop;
2126                                 int i, defaultfound = 0, totflag = 0;
2127
2128                                 if (eprop->item) {
2129                                         fprintf(f, "static EnumPropertyItem rna_%s%s_%s_items[%d] = {\n\t", srna->identifier, strnest, prop->identifier, eprop->totitem+1);
2130
2131                                         for (i = 0; i<eprop->totitem; i++) {
2132                                                 fprintf(f, "{%d, ", eprop->item[i].value);
2133                                                 rna_print_c_string(f, eprop->item[i].identifier); fprintf(f, ", ");
2134                                                 fprintf(f, "%d, ", eprop->item[i].icon);
2135                                                 rna_print_c_string(f, eprop->item[i].name); fprintf(f, ", ");
2136                                                 rna_print_c_string(f, eprop->item[i].description); fprintf(f, "},\n\t");
2137
2138                                                 if (eprop->item[i].identifier[0]) {
2139                                                         if (prop->flag & PROP_ENUM_FLAG) {
2140                                                                 totflag |= eprop->item[i].value;
2141                                                         }
2142                                                         else {
2143                                                                 if (eprop->defaultvalue == eprop->item[i].value) {
2144                                                                         defaultfound = 1;
2145                                                                 }
2146                                                         }
2147                                                 }
2148                                         }
2149
2150                                         fprintf(f, "{0, NULL, 0, NULL, NULL}\n};\n\n");
2151
2152                                         if (prop->flag & PROP_ENUM_FLAG) {
2153                                                 if (eprop->defaultvalue & ~totflag) {
2154                                                         fprintf(stderr, "%s: %s%s.%s, enum default includes unused bits (%d).\n",
2155                                                                 __func__, srna->identifier, errnest, prop->identifier, eprop->defaultvalue & ~totflag);
2156                                                         DefRNA.error = 1;
2157                                                 }
2158                                         }
2159                                         else {
2160                                                 if (!defaultfound) {
2161                                                         fprintf(stderr, "%s: %s%s.%s, enum default is not in items.\n",
2162                                                                 __func__, srna->identifier, errnest, prop->identifier);
2163                                                         DefRNA.error = 1;
2164                                                 }
2165                                         }
2166                                 }
2167                                 else {
2168                                         fprintf(stderr, "%s: %s%s.%s, enum must have items defined.\n",
2169                                                 __func__, srna->identifier, errnest, prop->identifier);
2170                                         DefRNA.error = 1;
2171                                 }
2172                                 break;
2173                         }
2174                         case PROP_BOOLEAN: {
2175                                 BoolPropertyRNA *bprop = (BoolPropertyRNA*)prop;
2176                                 unsigned int i;
2177
2178                                 if (prop->arraydimension && prop->totarraylength) {
2179                                         fprintf(f, "static int rna_%s%s_%s_default[%u] = {\n\t", srna->identifier, strnest, prop->identifier, prop->totarraylength);
2180
2181                                         for (i = 0; i<prop->totarraylength; i++) {
2182                                                 if (bprop->defaultarray)
2183                                                         fprintf(f, "%d", bprop->defaultarray[i]);
2184                                                 else
2185                                                         fprintf(f, "%d", bprop->defaultvalue);
2186                                                 if (i != prop->totarraylength-1)
2187                                                         fprintf(f, ",\n\t");
2188                                         }
2189
2190                                         fprintf(f, "\n};\n\n");
2191                                 }
2192                                 break;
2193                         }
2194                         case PROP_INT: {
2195                                 IntPropertyRNA *iprop = (IntPropertyRNA*)prop;
2196                                 unsigned int i;
2197
2198                                 if (prop->arraydimension && prop->totarraylength) {
2199                                         fprintf(f, "static int rna_%s%s_%s_default[%u] = {\n\t", srna->identifier, strnest, prop->identifier, prop->totarraylength);
2200
2201                                         for (i = 0; i<prop->totarraylength; i++) {
2202                                                 if (iprop->defaultarray)
2203                                                         fprintf(f, "%d", iprop->defaultarray[i]);
2204                                                 else
2205                                                         fprintf(f, "%d", iprop->defaultvalue);
2206                                                 if (i != prop->totarraylength-1)
2207                                                         fprintf(f, ",\n\t");
2208                                         }
2209
2210                                         fprintf(f, "\n};\n\n");
2211                                 }
2212                                 break;
2213                         }
2214                         case PROP_FLOAT: {
2215                                 FloatPropertyRNA *fprop = (FloatPropertyRNA*)prop;
2216                                 unsigned int i;
2217
2218                                 if (prop->arraydimension && prop->totarraylength) {
2219                                         fprintf(f, "static float rna_%s%s_%s_default[%u] = {\n\t", srna->identifier, strnest, prop->identifier, prop->totarraylength);
2220
2221                                         for (i = 0; i<prop->totarraylength; i++) {
2222                                                 if (fprop->defaultarray)
2223                                                         rna_float_print(f, fprop->defaultarray[i]);
2224                                                 else
2225                                                         rna_float_print(f, fprop->defaultvalue);
2226                                                 if (i != prop->totarraylength-1)
2227                                                         fprintf(f, ",\n\t");
2228                                         }
2229
2230                                         fprintf(f, "\n};\n\n");
2231                                 }
2232                                 break;
2233                         }
2234                         default:
2235                                 break;
2236         }
2237
2238         fprintf(f, "%s%s rna_%s%s_%s = {\n", (prop->flag & PROP_EXPORT)? "": "", rna_property_structname(prop->type), srna->identifier, strnest, prop->identifier);
2239
2240         if (prop->next) fprintf(f, "\t{(PropertyRNA*)&rna_%s%s_%s, ", srna->identifier, strnest, prop->next->identifier);
2241         else fprintf(f, "\t{NULL, ");
2242         if (prop->prev) fprintf(f, "(PropertyRNA*)&rna_%s%s_%s,\n", srna->identifier, strnest, prop->prev->identifier);
2243         else fprintf(f, "NULL,\n");
2244         fprintf(f, "\t%d, ", prop->magic);
2245         rna_print_c_string(f, prop->identifier);
2246         fprintf(f, ", %d, ", prop->flag);
2247         rna_print_c_string(f, prop->name); fprintf(f, ",\n\t");
2248         rna_print_c_string(f, prop->description); fprintf(f, ",\n\t");
2249         fprintf(f, "%d,\n", prop->icon);
2250         rna_print_c_string(f, prop->translation_context); fprintf(f, ",\n\t");
2251         fprintf(f, "\t%s, %s|%s, %s, %u, {%u, %u, %u}, %u,\n", RNA_property_typename(prop->type), rna_property_subtypename(prop->subtype), rna_property_subtype_unit(prop->subtype), rna_function_string(prop->getlength), prop->arraydimension, prop->arraylength[0], prop->arraylength[1], prop->arraylength[2], prop->totarraylength);
2252         fprintf(f, "\t%s%s, %d, %s, %s,\n", (prop->flag & PROP_CONTEXT_UPDATE)? "(UpdateFunc)": "", rna_function_string(prop->update), prop->noteflag, rna_function_string(prop->editable), rna_function_string(prop->itemeditable));
2253
2254         if (prop->flag & PROP_RAW_ACCESS) rna_set_raw_offset(f, srna, prop);
2255         else fprintf(f, "\t0, -1");
2256
2257         /* our own type - collections/arrays only */
2258         if (prop->srna) fprintf(f, ", &RNA_%s", (const char*)prop->srna);
2259         else fprintf(f, ", NULL");
2260
2261         fprintf(f, "},\n");
2262
2263         switch (prop->type) {
2264                         case PROP_BOOLEAN: {
2265                                 BoolPropertyRNA *bprop = (BoolPropertyRNA*)prop;
2266                                 fprintf(f, "\t%s, %s, %s, %s, %d, ", rna_function_string(bprop->get), rna_function_string(bprop->set), rna_function_string(bprop->getarray), rna_function_string(bprop->setarray), bprop->defaultvalue);
2267                                 if (prop->arraydimension && prop->totarraylength) fprintf(f, "rna_%s%s_%s_default\n", srna->identifier, strnest, prop->identifier);
2268                                 else fprintf(f, "NULL\n");
2269                                 break;
2270                         }
2271                         case PROP_INT: {
2272                                 IntPropertyRNA *iprop = (IntPropertyRNA*)prop;
2273                                 fprintf(f, "\t%s, %s, %s, %s, %s,\n\t", rna_function_string(iprop->get), rna_function_string(iprop->set), rna_function_string(iprop->getarray), rna_function_string(iprop->setarray), rna_function_string(iprop->range));
2274                                 rna_int_print(f, iprop->softmin); fprintf(f, ", ");
2275                                 rna_int_print(f, iprop->softmax); fprintf(f, ", ");
2276                                 rna_int_print(f, iprop->hardmin); fprintf(f, ", ");
2277                                 rna_int_print(f, iprop->hardmax); fprintf(f, ", ");
2278                                 rna_int_print(f, iprop->step); fprintf(f, ", ");
2279                                 rna_int_print(f, iprop->defaultvalue); fprintf(f, ", ");
2280                                 if (prop->arraydimension && prop->totarraylength) fprintf(f, "rna_%s%s_%s_default\n", srna->identifier, strnest, prop->identifier);
2281                                 else fprintf(f, "NULL\n");
2282                                 break;
2283                         }
2284                         case PROP_FLOAT: {
2285                                 FloatPropertyRNA *fprop = (FloatPropertyRNA*)prop;
2286                                 fprintf(f, "\t%s, %s, %s, %s, %s, ", rna_function_string(fprop->get), rna_function_string(fprop->set), rna_function_string(fprop->getarray), rna_function_string(fprop->setarray), rna_function_string(fprop->range));
2287                                 rna_float_print(f, fprop->softmin); fprintf(f, ", ");
2288                                 rna_float_print(f, fprop->softmax); fprintf(f, ", ");
2289                                 rna_float_print(f, fprop->hardmin); fprintf(f, ", ");
2290                                 rna_float_print(f, fprop->hardmax); fprintf(f, ", ");
2291                                 rna_float_print(f, fprop->step); fprintf(f, ", ");
2292                                 rna_int_print(f, (int)fprop->precision); fprintf(f, ", ");
2293                                 rna_float_print(f, fprop->defaultvalue); fprintf(f, ", ");
2294                                 if (prop->arraydimension && prop->totarraylength) fprintf(f, "rna_%s%s_%s_default\n", srna->identifier, strnest, prop->identifier);
2295                                 else fprintf(f, "NULL\n");
2296                                 break;
2297                         }
2298                         case PROP_STRING: {
2299                                 StringPropertyRNA *sprop = (StringPropertyRNA*)prop;
2300                                 fprintf(f, "\t%s, %s, %s, %d, ", rna_function_string(sprop->get), rna_function_string(sprop->length), rna_function_string(sprop->set), sprop->maxlength);
2301                                 rna_print_c_string(f, sprop->defaultvalue); fprintf(f, "\n");
2302                                 break;
2303                         }
2304                         case PROP_ENUM: {
2305                                 EnumPropertyRNA *eprop = (EnumPropertyRNA*)prop;
2306                                 fprintf(f, "\t%s, %s, %s, NULL, ", rna_function_string(eprop->get), rna_function_string(eprop->set), rna_function_string(eprop->itemf));
2307                                 if (eprop->item)
2308                                         fprintf(f, "rna_%s%s_%s_items, ", srna->identifier, strnest, prop->identifier);
2309                                 else
2310                                         fprintf(f, "NULL, ");
2311                                 fprintf(f, "%d, %d\n", eprop->totitem, eprop->defaultvalue);
2312                                 break;
2313                         }
2314                         case PROP_POINTER: {
2315                                 PointerPropertyRNA *pprop = (PointerPropertyRNA*)prop;
2316                                 fprintf(f, "\t%s, %s, %s, %s,", rna_function_string(pprop->get), rna_function_string(pprop->set), rna_function_string(pprop->typef), rna_function_string(pprop->poll));
2317                                 if (pprop->type) fprintf(f, "&RNA_%s\n", (const char*)pprop->type);
2318                                 else fprintf(f, "NULL\n");
2319                                 break;
2320                         }
2321                         case PROP_COLLECTION: {
2322                                 CollectionPropertyRNA *cprop = (CollectionPropertyRNA*)prop;
2323                                 fprintf(f, "\t%s, %s, %s, %s, %s, %s, %s, %s, ", rna_function_string(cprop->begin), rna_function_string(cprop->next), rna_function_string(cprop->end), rna_function_string(cprop->get), rna_function_string(cprop->length), rna_function_string(cprop->lookupint), rna_function_string(cprop->lookupstring), rna_function_string(cprop->assignint));
2324                                 if (cprop->item_type) fprintf(f, "&RNA_%s\n", (const char*)cprop->item_type);
2325                                 else fprintf(f, "NULL\n");
2326                                 break;
2327                         }
2328         }
2329
2330         fprintf(f, "};\n\n");
2331
2332         if (freenest) {
2333                 MEM_freeN(strnest);
2334                 MEM_freeN(errnest);
2335         }
2336 }
2337
2338 static void rna_generate_struct(BlenderRNA *brna, StructRNA *srna, FILE *f)
2339 {
2340         FunctionRNA *func;
2341         FunctionDefRNA *dfunc;
2342         PropertyRNA *prop, *parm;
2343         StructRNA *base;
2344
2345         fprintf(f, "/* %s */\n", srna->name);
2346
2347         for (prop = srna->cont.properties.first; prop; prop = prop->next)
2348                 rna_generate_property(f, srna, NULL, prop);
2349
2350         for (func = srna->functions.first; func; func = func->cont.next) {
2351                 for (parm = func->cont.properties.first; parm; parm = parm->next)
2352                         rna_generate_property(f, srna, func->identifier, parm);
2353
2354                 fprintf(f, "%s%s rna_%s_%s_func = {\n", "", "FunctionRNA", srna->identifier, func->identifier);
2355
2356                 if (func->cont.next) fprintf(f, "\t{(FunctionRNA*)&rna_%s_%s_func, ", srna->identifier, ((FunctionRNA*)func->cont.next)->identifier);
2357                 else fprintf(f, "\t{NULL, ");
2358                 if (func->cont.prev) fprintf(f, "(FunctionRNA*)&rna_%s_%s_func,\n", srna->identifier, ((FunctionRNA*)func->cont.prev)->identifier);
2359                 else fprintf(f, "NULL,\n");
2360
2361                 fprintf(f, "\tNULL,\n");
2362
2363                 parm = func->cont.properties.first;
2364                 if (parm) fprintf(f, "\t{(PropertyRNA*)&rna_%s_%s_%s, ", srna->identifier, func->identifier, parm->identifier);
2365                 else fprintf(f, "\t{NULL, ");
2366
2367                 parm = func->cont.properties.last;
2368                 if (parm) fprintf(f, "(PropertyRNA*)&rna_%s_%s_%s}},\n", srna->identifier, func->identifier, parm->identifier);
2369                 else fprintf(f, "NULL}},\n");
2370
2371                 fprintf(f, "\t");
2372                 rna_print_c_string(f, func->identifier);
2373                 fprintf(f, ", %d, ", func->flag);
2374                 rna_print_c_string(f, func->description); fprintf(f, ",\n");
2375
2376                 dfunc = rna_find_function_def(func);
2377                 if (dfunc->gencall) fprintf(f, "\t%s,\n", dfunc->gencall);
2378                 else fprintf(f, "\tNULL,\n");
2379
2380                 if (func->c_ret) fprintf(f, "\t(PropertyRNA*)&rna_%s_%s_%s\n", srna->identifier, func->identifier, func->c_ret->identifier);
2381                 else fprintf(f, "\tNULL\n");
2382
2383                 fprintf(f, "};\n");
2384                 fprintf(f, "\n");
2385         }
2386
2387         fprintf(f, "StructRNA RNA_%s = {\n", srna->identifier);
2388
2389         if (srna->cont.next) fprintf(f, "\t{(ContainerRNA *)&RNA_%s, ", ((StructRNA*)srna->cont.next)->identifier);
2390         else fprintf(f, "\t{NULL, ");
2391         if (srna->cont.prev) fprintf(f, "(ContainerRNA *)&RNA_%s,\n", ((StructRNA*)srna->cont.prev)->identifier);
2392         else fprintf(f, "NULL,\n");
2393
2394         fprintf(f, "\tNULL,\n");
2395
2396         prop = srna->cont.properties.first;
2397         if (prop) fprintf(f, "\t{(PropertyRNA*)&rna_%s_%s, ", srna->identifier, prop->identifier);
2398         else fprintf(f, "\t{NULL, ");
2399
2400         prop = srna->cont.properties.last;
2401         if (prop) fprintf(f, "(PropertyRNA*)&rna_%s_%s}},\n", srna->identifier, prop->identifier);
2402         else fprintf(f, "NULL}},\n");
2403         fprintf(f, "\t");
2404         rna_print_c_string(f, srna->identifier);
2405         fprintf(f, "\t, NULL,NULL\n"); /* PyType - Cant initialize here */
2406         fprintf(f, ", %d, ", srna->flag);
2407         rna_print_c_string(f, srna->name);
2408         fprintf(f, ", ");
2409         rna_print_c_string(f, srna->description);
2410         fprintf(f, ",\n\t%d,\n", srna->icon);
2411
2412         prop = srna->nameproperty;
2413         if (prop) {
2414                 base = srna;
2415                 while (base->base && base->base->nameproperty == prop)
2416                         base = base->base;
2417
2418                 fprintf(f, "\t(PropertyRNA*)&rna_%s_%s, ", base->identifier, prop->identifier);
2419         }
2420         else fprintf(f, "\tNULL, ");
2421
2422         prop = srna->iteratorproperty;
2423         base = srna;
2424         while (base->base && base->base->iteratorproperty == prop)
2425                 base = base->base;
2426         fprintf(f, "(PropertyRNA*)&rna_%s_rna_properties,\n", base->identifier);
2427
2428         if (srna->base) fprintf(f, "\t&RNA_%s,\n", srna->base->identifier);
2429         else fprintf(f, "\tNULL,\n");
2430
2431         if (srna->nested) fprintf(f, "\t&RNA_%s,\n", srna->nested->identifier);
2432         else fprintf(f, "\tNULL,\n");
2433
2434         fprintf(f, "\t%s,\n", rna_function_string(srna->refine));
2435         fprintf(f, "\t%s,\n", rna_function_string(srna->path));
2436         fprintf(f, "\t%s,\n", rna_function_string(srna->reg));
2437         fprintf(f, "\t%s,\n", rna_function_string(srna->unreg));
2438         fprintf(f, "\t%s,\n", rna_function_string(srna->instance));
2439         fprintf(f, "\t%s,\n", rna_function_string(srna->idproperties));
2440
2441         if (srna->reg && !srna->refine) {
2442                 fprintf(stderr, "%s: %s has a register function, must also have refine function.\n",
2443                         __func__, srna->identifier);
2444                 DefRNA.error = 1;
2445         }
2446
2447         func = srna->functions.first;
2448         if (func) fprintf(f, "\t{(FunctionRNA*)&rna_%s_%s_func, ", srna->identifier, func->identifier);
2449         else fprintf(f, "\t{NULL, ");
2450
2451         func = srna->functions.last;
2452         if (func) fprintf(f, "(FunctionRNA*)&rna_%s_%s_func}\n", srna->identifier, func->identifier);
2453         else fprintf(f, "NULL}\n");
2454
2455         fprintf(f, "};\n");
2456
2457         fprintf(f, "\n");
2458 }
2459
2460 typedef struct RNAProcessItem {
2461         const char *filename;
2462         const char *api_filename;
2463         void (*define)(BlenderRNA *brna);
2464 } RNAProcessItem;
2465
2466 static RNAProcessItem PROCESS_ITEMS[] = {
2467         {"rna_rna.c", NULL, RNA_def_rna},
2468         {"rna_ID.c", NULL, RNA_def_ID},
2469         {"rna_texture.c", "rna_texture_api.c", RNA_def_texture},
2470         {"rna_action.c", "rna_action_api.c", RNA_def_action},
2471         {"rna_animation.c", "rna_animation_api.c", RNA_def_animation},
2472         {"rna_animviz.c", NULL, RNA_def_animviz},
2473         {"rna_actuator.c", "rna_actuator_api.c", RNA_def_actuator},
2474         {"rna_armature.c", "rna_armature_api.c", RNA_def_armature},
2475         {"rna_boid.c", NULL, RNA_def_boid},
2476         {"rna_brush.c", NULL, RNA_def_brush},
2477         {"rna_camera.c", "rna_camera_api.c", RNA_def_camera},
2478         {"rna_cloth.c", NULL, RNA_def_cloth},
2479         {"rna_color.c", NULL, RNA_def_color},
2480         {"rna_constraint.c", NULL, RNA_def_constraint},
2481         {"rna_context.c", NULL, RNA_def_context},
2482         {"rna_controller.c", "rna_controller_api.c", RNA_def_controller},
2483         {"rna_curve.c", NULL, RNA_def_curve},
2484         {"rna_dynamicpaint.c", NULL, RNA_def_dynamic_paint},
2485         {"rna_fcurve.c", "rna_fcurve_api.c", RNA_def_fcurve},
2486         {"rna_fluidsim.c", NULL, RNA_def_fluidsim},
2487         {"rna_gpencil.c", NULL, RNA_def_gpencil},
2488         {"rna_group.c", NULL, RNA_def_group},
2489         {"rna_image.c", "rna_image_api.c", RNA_def_image},
2490         {"rna_key.c", NULL, RNA_def_key},
2491         {"rna_lamp.c", NULL, RNA_def_lamp},
2492         {"rna_lattice.c", NULL, RNA_def_lattice},
2493         {"rna_main.c", "rna_main_api.c", RNA_def_main},
2494         {"rna_material.c", "rna_material_api.c", RNA_def_material},
2495         {"rna_mesh.c", "rna_mesh_api.c", RNA_def_mesh},
2496         {"rna_meta.c", NULL, RNA_def_meta},
2497         {"rna_modifier.c", NULL, RNA_def_modifier},
2498         {"rna_nla.c", NULL, RNA_def_nla},
2499         {"rna_nodetree.c", NULL, RNA_def_nodetree},
2500         {"rna_object.c", "rna_object_api.c", RNA_def_object},
2501         {"rna_object_force.c", NULL, RNA_def_object_force},
2502         {"rna_packedfile.c", NULL, RNA_def_packedfile},
2503         {"rna_particle.c", NULL, RNA_def_particle},
2504         {"rna_pose.c", "rna_pose_api.c", RNA_def_pose},
2505         {"rna_property.c", NULL, RNA_def_gameproperty},
2506         {"rna_render.c", NULL, RNA_def_render},
2507         {"rna_scene.c", "rna_scene_api.c", RNA_def_scene},
2508         {"rna_screen.c", NULL, RNA_def_screen},
2509         {"rna_sculpt_paint.c", NULL, RNA_def_sculpt_paint},
2510         {"rna_sensor.c", "rna_sensor_api.c", RNA_def_sensor},
2511         {"rna_sequencer.c", "rna_sequencer_api.c", RNA_def_sequencer},
2512         {"rna_smoke.c", NULL, RNA_def_smoke},
2513         {"rna_space.c", NULL, RNA_def_space},
2514         {"rna_speaker.c", NULL, RNA_def_speaker},
2515         {"rna_test.c", NULL, RNA_def_test},
2516         {"rna_text.c", "rna_text_api.c", RNA_def_text},
2517         {"rna_timeline.c", NULL, RNA_def_timeline_marker},
2518         {"rna_sound.c", NULL, RNA_def_sound},
2519         {"rna_ui.c", "rna_ui_api.c", RNA_def_ui},
2520         {"rna_userdef.c", NULL, RNA_def_userdef},
2521         {"rna_vfont.c", NULL, RNA_def_vfont},
2522         {"rna_wm.c", "rna_wm_api.c", RNA_def_wm},
2523         {"rna_world.c", NULL, RNA_def_world},   
2524         {"rna_movieclip.c", NULL, RNA_def_movieclip},
2525         {"rna_tracking.c", NULL, RNA_def_tracking},
2526         {NULL, NULL}};
2527
2528 static void rna_generate(BlenderRNA *brna, FILE *f, const char *filename, const char *api_filename)
2529 {
2530         StructDefRNA *ds;
2531         PropertyDefRNA *dp;
2532         FunctionDefRNA *dfunc;
2533         
2534         fprintf(f, "\n/* Automatically generated struct definitions for the Data API.\n"
2535                                  "   Do not edit manually, changes will be overwritten.           */\n\n"
2536                                   "#define RNA_RUNTIME\n\n");
2537
2538         fprintf(f, "#include <float.h>\n");
2539         fprintf(f, "#include <stdio.h>\n");
2540         fprintf(f, "#include <limits.h>\n");
2541         fprintf(f, "#include <string.h>\n\n");
2542         fprintf(f, "#include <stddef.h>\n\n");
2543
2544         fprintf(f, "#include \"DNA_ID.h\"\n");
2545         fprintf(f, "#include \"DNA_scene_types.h\"\n");
2546
2547         fprintf(f, "#include \"BLI_blenlib.h\"\n\n");
2548         fprintf(f, "#include \"BLI_utildefines.h\"\n\n");
2549
2550         fprintf(f, "#include \"BKE_context.h\"\n");
2551         fprintf(f, "#include \"BKE_library.h\"\n");
2552         fprintf(f, "#include \"BKE_main.h\"\n");
2553         fprintf(f, "#include \"BKE_report.h\"\n");
2554
2555         fprintf(f, "#include \"RNA_define.h\"\n");
2556         fprintf(f, "#include \"RNA_types.h\"\n");
2557         fprintf(f, "#include \"rna_internal.h\"\n\n");
2558
2559         rna_generate_prototypes(brna, f);
2560
2561         fprintf(f, "#include \"%s\"\n", filename);
2562         if (api_filename)
2563                 fprintf(f, "#include \"%s\"\n", api_filename);
2564         fprintf(f, "\n");
2565
2566         fprintf(f, "/* Autogenerated Functions */\n\n");
2567
2568         for (ds = DefRNA.structs.first; ds; ds = ds->cont.next) {
2569                 if (!filename || ds->filename == filename) {
2570                         rna_generate_property_prototypes(brna, ds->srna, f);
2571                         rna_generate_function_prototypes(brna, ds->srna, f);
2572                 }
2573         }
2574
2575         for (ds = DefRNA.structs.first; ds; ds = ds->cont.next)
2576                 if (!filename || ds->filename == filename)
2577                         for (dp = ds->cont.properties.first; dp; dp = dp->next)
2578                                 rna_def_property_funcs(f, ds->srna, dp);
2579
2580         for (ds = DefRNA.structs.first; ds; ds = ds->cont.next) {
2581                 if (!filename || ds->filename == filename) {
2582                         for (dfunc = ds->functions.first; dfunc; dfunc = dfunc->cont.next)
2583                                 rna_def_function_funcs(f, ds, dfunc);
2584
2585                         rna_generate_static_function_prototypes(brna, ds->srna, f);
2586                 }
2587         }
2588
2589         for (ds = DefRNA.structs.first; ds; ds = ds->cont.next)
2590                 if (!filename || ds->filename == filename)
2591                         rna_generate_struct(brna, ds->srna, f);
2592
2593         if (strcmp(filename, "rna_ID.c") == 0) {
2594                 /* this is ugly, but we cannot have c files compiled for both
2595                  * makesrna and blender with some build systems at the moment */
2596                 fprintf(f, "#include \"rna_define.c\"\n\n");
2597
2598                 rna_generate_blender(brna, f);
2599         }
2600 }
2601
2602 static void rna_generate_header(BlenderRNA *brna, FILE *f)
2603 {
2604         StructDefRNA *ds;
2605         PropertyDefRNA *dp;
2606         StructRNA *srna;
2607
2608         fprintf(f, "\n#ifndef __RNA_BLENDER_H__\n");
2609         fprintf(f, "#define __RNA_BLENDER_H__\n\n");
2610
2611         fprintf(f, "/* Automatically generated function declarations for the Data API.\n"
2612                                  "   Do not edit manually, changes will be overwritten.              */\n\n");
2613
2614         fprintf(f, "#include \"RNA_types.h\"\n\n");
2615
2616         fprintf(f, "#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n");
2617
2618         fprintf(f, "#define FOREACH_BEGIN(property, sptr, itemptr) \\\n");
2619         fprintf(f, "    { \\\n");
2620         fprintf(f, "            CollectionPropertyIterator rna_macro_iter; \\\n");
2621         fprintf(f, "            for(property##_begin(&rna_macro_iter, sptr); rna_macro_iter.valid; property##_next(&rna_macro_iter)) { \\\n");
2622         fprintf(f, "                    itemptr= rna_macro_iter.ptr;\n\n");
2623
2624         fprintf(f, "#define FOREACH_END(property) \\\n");
2625         fprintf(f, "            } \\\n");
2626         fprintf(f, "            property##_end(&rna_macro_iter); \\\n");
2627         fprintf(f, "    }\n\n");
2628
2629         for (ds = DefRNA.structs.first; ds; ds = ds->cont.next) {
2630                 srna = ds->srna;
2631
2632                 fprintf(f, "/**************** %s ****************/\n\n", srna->name);
2633
2634                 while (srna) {
2635                         fprintf(f, "extern StructRNA RNA_%s;\n", srna->identifier);
2636                         srna = srna->base;
2637                 }
2638                 fprintf(f, "\n");
2639
2640                 for (dp = ds->cont.properties.first; dp; dp = dp->next)
2641                         rna_def_property_funcs_header(f, ds->srna, dp);
2642         }
2643
2644         fprintf(f, "#ifdef __cplusplus\n}\n#endif\n\n");
2645
2646         fprintf(f, "#endif /* __RNA_BLENDER_H__ */\n\n");
2647 }
2648
2649 static const char *cpp_classes = ""
2650 "\n"
2651 "#include <string>\n"
2652 "#include <string.h> /* for memcpy */\n"
2653 "\n"
2654 "namespace BL {\n"
2655 "\n"
2656 "#define BOOLEAN_PROPERTY(sname, identifier) \\\n"
2657 "       inline bool sname::identifier(void) { return sname##_##identifier##_get(&ptr)? true: false; }\n"
2658 "\n"
2659 "#define BOOLEAN_ARRAY_PROPERTY(sname, size, identifier) \\\n"
2660 "       inline Array<int,size> sname::identifier(void) \\\n"
2661 "               { Array<int, size> ar; sname##_##identifier##_get(&ptr, ar.data); return ar; }\n"
2662 "\n"
2663 "#define INT_PROPERTY(sname, identifier) \\\n"
2664 "       inline int sname::identifier(void) { return sname##_##identifier##_get(&ptr); }\n"
2665 "\n"
2666 "#define INT_ARRAY_PROPERTY(sname, size, identifier) \\\n"
2667 "       inline Array<int,size> sname::identifier(void) \\\n"
2668 "               { Array<int, size> ar; sname##_##identifier##_get(&ptr, ar.data); return ar; }\n"
2669 "\n"
2670 "#define FLOAT_PROPERTY(sname, identifier) \\\n"
2671 "       inline float sname::identifier(void) { return sname##_##identifier##_get(&ptr); }\n"
2672 "\n"
2673 "#define FLOAT_ARRAY_PROPERTY(sname, size, identifier) \\\n"
2674 "       inline Array<float,size> sname::identifier(void) \\\n"
2675 "               { Array<float, size> ar; sname##_##identifier##_get(&ptr, ar.data); return ar; }\n"
2676 "\n"
2677 "#define ENUM_PROPERTY(type, sname, identifier) \\\n"
2678 "       inline sname::type sname::identifier(void) { return (type)sname##_##identifier##_get(&ptr); }\n"
2679 "\n"
2680 "#define STRING_PROPERTY(sname, identifier) \\\n"
2681 "       inline std::string sname::identifier(void) { \\\n"
2682 "               int len= sname##_##identifier##_length(&ptr); \\\n"
2683 "               std::string str; str.resize(len); \\\n"
2684 "               sname##_##identifier##_get(&ptr, &str[0]); return str; } \\\n"
2685 "\n"
2686 "#define POINTER_PROPERTY(type, sname, identifier) \\\n"
2687 "       inline type sname::identifier(void) { return type(sname##_##identifier##_get(&ptr)); }\n"
2688 "\n"
2689 "#define COLLECTION_PROPERTY(type, sname, identifier) \\\n"
2690 "       typedef CollectionIterator<type, sname##_##identifier##_begin, \\\n"
2691 "               sname##_##identifier##_next, sname##_##identifier##_end> identifier##_iterator; \\\n"
2692 "       Collection<sname, type, sname##_##identifier##_begin, \\\n"
2693 "               sname##_##identifier##_next, sname##_##identifier##_end> identifier;\n"
2694 "\n"
2695 "class Pointer {\n"
2696 "public:\n"
2697 "       Pointer(const PointerRNA& p) : ptr(p) { }\n"
2698 "       operator const PointerRNA&() { return ptr; }\n"
2699 "       bool is_a(StructRNA *type) { return RNA_struct_is_a(ptr.type, type)? true: false; }\n"
2700 "       operator void*() { return ptr.data; }\n"
2701 "       operator bool() { return ptr.data != NULL; }\n"
2702 "\n"
2703 "       PointerRNA ptr;\n"
2704 "};\n"
2705 "\n"
2706 "\n"
2707 "template<typename T, int Tsize>\n"
2708 "class Array {\n"
2709 "public:\n"
2710 "       T data[Tsize];\n"
2711 "\n"
2712 "   Array() {}\n"
2713 "       Array(const Array<T, Tsize>& other) { memcpy(data, other.data, sizeof(T)*Tsize); }\n"
2714 "       const Array<T, Tsize>& operator=(const Array<T, Tsize>& other) { memcpy(data, other.data, sizeof(T)*Tsize); return *this; }\n"
2715 "\n"
2716 "       operator T*() { return data; }\n"
2717 "};\n"
2718 "\n"
2719 "typedef void (*TBeginFunc)(CollectionPropertyIterator *iter, PointerRNA *ptr);\n"
2720 "typedef void (*TNextFunc)(CollectionPropertyIterator *iter);\n"
2721 "typedef void (*TEndFunc)(CollectionPropertyIterator *iter);\n"
2722 "\n"
2723 "template<typename T, TBeginFunc Tbegin, TNextFunc Tnext, TEndFunc Tend>\n"
2724 "class CollectionIterator {\n"
2725 "public:\n"
2726 "       CollectionIterator() : t(iter.ptr), init(false) { iter.valid= false; }\n"
2727 "       ~CollectionIterator(void) { if(init) Tend(&iter); };\n"
2728 "\n"
2729 "       operator bool(void)\n"
2730 "       { return iter.valid != 0; }\n"
2731 "       const CollectionIterator<T, Tbegin, Tnext, Tend>& operator++() { Tnext(&iter); t = T(iter.ptr); return *this; }\n"
2732 "\n"
2733 "       T& operator*(void) { return t; }\n"
2734 "       T* operator->(void) { return &t; }\n"
2735 "       bool operator==(const CollectionIterator<T, Tbegin, Tnext, Tend>& other) { return iter.valid == other.iter.valid; }\n"
2736 "       bool operator!=(const CollectionIterator<T, Tbegin, Tnext, Tend>& other) { return iter.valid != other.iter.valid; }\n"
2737 "\n"
2738 "       void begin(const Pointer& ptr)\n"
2739 "       { if(init) Tend(&iter); Tbegin(&iter, (PointerRNA*)&ptr.ptr); t = T(iter.ptr); init = true; }\n"
2740 "\n"
2741 "private:\n"
2742 "       const CollectionIterator<T, Tbegin, Tnext, Tend>& operator=(const CollectionIterator<T, Tbegin, Tnext, Tend>& copy) {}\n"
2743 ""
2744 "       CollectionPropertyIterator iter;\n"
2745 "       T t;\n"
2746 "       bool init;\n"
2747 "};\n"
2748 "\n"
2749 "template<typename Tp, typename T, TBeginFunc Tbegin, TNextFunc Tnext, TEndFunc Tend>\n"
2750 "class Collection {\n"
2751 "public:\n"
2752 "       Collection(const PointerRNA& p) : ptr(p) {}\n"
2753 "\n"
2754 "       void begin(CollectionIterator<T, Tbegin, Tnext, Tend>& iter)\n"
2755 "       { iter.begin(ptr); }\n"
2756 "       CollectionIterator<T, Tbegin, Tnext, Tend> end()\n"
2757 "       { return CollectionIterator<T, Tbegin, Tnext, Tend>(); } /* test */ \n"
2758 "\n"
2759 "private:\n"
2760 "       PointerRNA ptr;\n"
2761 "};\n"
2762 "\n";
2763
2764 static void rna_generate_header_cpp(BlenderRNA *brna, FILE *f)
2765 {
2766         StructDefRNA *ds;
2767         PropertyDefRNA *dp;
2768         StructRNA *srna;
2769
2770         fprintf(f, "\n#ifndef __RNA_BLENDER_CPP_H__\n");
2771         fprintf(f, "#define __RNA_BLENDER_CPP_H__\n\n");
2772
2773         fprintf(f, "/* Automatically generated classes for the Data API.\n"
2774                                  "   Do not edit manually, changes will be overwritten. */\n\n");
2775         
2776         fprintf(f, "#include \"RNA_blender.h\"\n");
2777         fprintf(f, "#include \"RNA_types.h\"\n");
2778         fprintf(f, "#include \"RNA_access.h\"\n");
2779
2780         fprintf(f, "%s", cpp_classes);
2781
2782         fprintf(f, "/**************** Declarations ****************/\n\n");
2783
2784         for (ds = DefRNA.structs.first; ds; ds = ds->cont.next)
2785                 fprintf(f, "class %s;\n", ds->srna->identifier);
2786         fprintf(f, "\n");
2787
2788         for (ds = DefRNA.structs.first; ds; ds = ds->cont.next) {
2789                 srna = ds->srna;
2790
2791                 fprintf(f, "/**************** %s ****************/\n\n", srna->name);
2792
2793                 fprintf(f, "class %s : public %s {\n", srna->identifier, (srna->base)? srna->base->identifier: "Pointer");
2794                 fprintf(f, "public:\n");
2795                 fprintf(f, "\t%s(const PointerRNA& ptr) :\n\t\t%s(ptr)", srna->identifier, (srna->base)? srna->base->identifier: "Pointer");
2796                 for (dp = ds->cont.properties.first; dp; dp = dp->next)
2797                         if (!(dp->prop->flag & (PROP_IDPROPERTY|PROP_BUILTIN)))
2798                                 if (dp->prop->type == PROP_COLLECTION)
2799                                         fprintf(f, ",\n\t\t%s(ptr)", dp->prop->identifier);
2800                 fprintf(f, "\n\t\t{}\n\n");
2801
2802                 for (dp = ds->cont.properties.first; dp; dp = dp->next)
2803                         rna_def_property_funcs_header_cpp(f, ds->srna, dp);
2804                 fprintf(f, "};\n\n");
2805         }
2806
2807
2808         fprintf(f, "/**************** Implementation ****************/\n");
2809
2810         for (ds = DefRNA.structs.first; ds; ds = ds->cont.next) {
2811                 for (dp = ds->cont.properties.first; dp; dp = dp->next)
2812                         rna_def_property_funcs_impl_cpp(f, ds->srna, dp);
2813
2814                 fprintf(f, "\n");
2815         }
2816
2817         fprintf(f, "}\n\n#endif /* __RNA_BLENDER_CPP_H__ */\n\n");
2818 }
2819
2820 static void make_bad_file(const char *file, int line)
2821 {
2822         FILE *fp = fopen(file, "w");
2823         fprintf(fp, "#error \"Error! can't make correct RNA file from %s:%d, STUPID!\"\n", __FILE__, line);
2824         fclose(fp);
2825 }
2826
2827 static int rna_preprocess(const char *outfile)
2828 {
2829         BlenderRNA *brna;
2830         StructDefRNA *ds;
2831         FILE *file;
2832         char deffile[4096];
2833         int i, status;
2834         const char *deps[3]; /* expand as needed */
2835
2836         /* define rna */
2837         brna = RNA_create();
2838
2839         for (i = 0; PROCESS_ITEMS[i].filename; i++) {
2840                 if (PROCESS_ITEMS[i].define) {
2841                         PROCESS_ITEMS[i].define(brna);
2842
2843                         for (ds = DefRNA.structs.first; ds; ds = ds->cont.next)
2844                                 if (!ds->filename)
2845                                         ds->filename = PROCESS_ITEMS[i].filename;
2846                 }
2847         }
2848
2849         rna_auto_types();
2850
2851
2852         /* create RNA_blender_cpp.h */
2853         strcpy(deffile, outfile);
2854         strcat(deffile, "RNA_blender_cpp.h" TMP_EXT);
2855
2856         status = (DefRNA.error != 0);
2857
2858         if (status) {
2859                 make_bad_file(deffile, __LINE__);
2860         }
2861         else {
2862                 file = fopen(deffile, "w");
2863
2864                 if (!file) {
2865                         fprintf(stderr, "Unable to open file: %s\n", deffile);
2866                         status = 1;
2867                 }
2868                 else {
2869                         rna_generate_header_cpp(brna, file);
2870                         fclose(file);
2871                         status = (DefRNA.error != 0);
2872                 }
2873         }
2874
2875         replace_if_different(deffile, NULL);
2876
2877         rna_sort(brna);
2878
2879         /* create rna_gen_*.c files */
2880         for (i = 0; PROCESS_ITEMS[i].filename; i++) {
2881                 strcpy(deffile, outfile);
2882                 strcat(deffile, PROCESS_ITEMS[i].filename);
2883                 deffile[strlen(deffile)-2] = '\0';
2884                 strcat(deffile, "_gen.c" TMP_EXT);
2885
2886                 if (status) {
2887                         make_bad_file(deffile, __LINE__);
2888                 }
2889                 else {
2890                         file = fopen(deffile, "w");
2891
2892                         if (!file) {
2893                                 fprintf(stderr, "Unable to open file: %s\n", deffile);
2894                                 status = 1;
2895                         }
2896                         else {
2897                                 rna_generate(brna, file, PROCESS_ITEMS[i].filename, PROCESS_ITEMS[i].api_filename);
2898                                 fclose(file);
2899                                 status = (DefRNA.error != 0);
2900                         }
2901                 }
2902
2903                 /* avoid unneeded rebuilds */
2904                 deps[0] = PROCESS_ITEMS[i].filename;