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