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