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