Merged revision(s) 58682-58770 from trunk/blender into soc-2013-dingto.
[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, "    int i, arraylen[RNA_MAX_ARRAY_DIMENSION];\n");
650                                                 fprintf(f, "    int len = %s(ptr, arraylen);\n\n", lenfunc);
651                                                 fprintf(f, "    for (i = 0; i < len; i++) {\n");
652                                                 MEM_freeN(lenfunc);
653                                         }
654                                         else {
655                                                 fprintf(f, "    int i;\n\n");
656                                                 fprintf(f, "    for (i = 0; i < %u; i++) {\n", prop->totarraylength);
657                                         }
658
659                                         if (dp->dnaarraylength == 1) {
660                                                 if (prop->type == PROP_BOOLEAN && dp->booleanbit) {
661                                                         fprintf(f, "            values[i] = %s((data->%s & (%d << i)) != 0);\n",
662                                                                 (dp->booleannegative) ? "!" : "", dp->dnaname, dp->booleanbit);
663                                                 }
664                                                 else {
665                                                         fprintf(f, "            values[i] = (%s)%s((&data->%s)[i]);\n",
666                                                                 rna_type_type(prop), (dp->booleannegative) ? "!" : "", dp->dnaname);
667                                                 }
668                                         }
669                                         else {
670                                                 if (prop->type == PROP_BOOLEAN && dp->booleanbit) {
671                                                         fprintf(f, "            values[i] = %s((data->%s[i] & ", (dp->booleannegative) ? "!" : "",
672                                                                 dp->dnaname);
673                                                         rna_int_print(f, dp->booleanbit);
674                                                         fprintf(f, ") != 0);\n");
675                                                 }
676                                                 else if (rna_color_quantize(prop, dp)) {
677                                                         fprintf(f, "            values[i] = (%s)(data->%s[i] * (1.0f / 255.0f));\n",
678                                                                 rna_type_type(prop), dp->dnaname);
679                                                 }
680                                                 else if (dp->dnatype) {
681                                                         fprintf(f, "            values[i] = (%s)%s(((%s *)data->%s)[i]);\n",
682                                                                 rna_type_type(prop), (dp->booleannegative) ? "!" : "", dp->dnatype, dp->dnaname);
683                                                 }
684                                                 else {
685                                                         fprintf(f, "            values[i] = (%s)%s((data->%s)[i]);\n",
686                                                                 rna_type_type(prop), (dp->booleannegative) ? "!" : "", dp->dnaname);
687                                                 }
688                                         }
689                                         fprintf(f, "    }\n");
690                                 }
691                                 fprintf(f, "}\n\n");
692                         }
693                         else {
694                                 fprintf(f, "%s %s(PointerRNA *ptr)\n", rna_type_type(prop), func);
695                                 fprintf(f, "{\n");
696
697                                 if (manualfunc) {
698                                         fprintf(f, "    return %s(ptr);\n", manualfunc);
699                                 }
700                                 else {
701                                         rna_print_data_get(f, dp);
702                                         if (prop->type == PROP_BOOLEAN && dp->booleanbit) {
703                                                 fprintf(f, "    return %s(((data->%s) & ", (dp->booleannegative) ? "!" : "", dp->dnaname);
704                                                 rna_int_print(f, dp->booleanbit);
705                                                 fprintf(f, ") != 0);\n");
706                                         }
707                                         else if (prop->type == PROP_ENUM && dp->enumbitflags) {
708                                                 fprintf(f, "    return ((data->%s) & ", dp->dnaname);
709                                                 rna_int_print(f, rna_enum_bitmask(prop));
710                                                 fprintf(f, ");\n");
711                                         }
712                                         else
713                                                 fprintf(f, "    return (%s)%s(data->%s);\n", rna_type_type(prop),
714                                                         (dp->booleannegative) ? "!" : "", dp->dnaname);
715                                 }
716
717                                 fprintf(f, "}\n\n");
718                         }
719                         break;
720         }
721
722         return func;
723 }
724
725 /* defined min/max variables to be used by rna_clamp_value() */
726 static void rna_clamp_value_range(FILE *f, PropertyRNA *prop)
727 {
728         if (prop->type == PROP_FLOAT) {
729                 FloatPropertyRNA *fprop = (FloatPropertyRNA *)prop;
730                 if (fprop->range) {
731                         fprintf(f,
732                                 "       float prop_clamp_min = -FLT_MAX, prop_clamp_max = FLT_MAX, prop_soft_min, prop_soft_max;\n");
733                         fprintf(f, "    %s(ptr, &prop_clamp_min, &prop_clamp_max, &prop_soft_min, &prop_soft_max);\n",
734                                 rna_function_string(fprop->range));
735                 }
736         }
737         else if (prop->type == PROP_INT) {
738                 IntPropertyRNA *iprop = (IntPropertyRNA *)prop;
739                 if (iprop->range) {
740                         fprintf(f, "    int prop_clamp_min = INT_MIN, prop_clamp_max = INT_MAX, prop_soft_min, prop_soft_max;\n");
741                         fprintf(f, "    %s(ptr, &prop_clamp_min, &prop_clamp_max, &prop_soft_min, &prop_soft_max);\n",
742                                 rna_function_string(iprop->range));
743                 }
744         }
745 }
746
747 static void rna_clamp_value(FILE *f, PropertyRNA *prop, int array)
748 {
749         if (prop->type == PROP_INT) {
750                 IntPropertyRNA *iprop = (IntPropertyRNA *)prop;
751
752                 if (iprop->hardmin != INT_MIN || iprop->hardmax != INT_MAX || iprop->range) {
753                         if (array) fprintf(f, "CLAMPIS(values[i], ");
754                         else fprintf(f, "CLAMPIS(value, ");
755                         if (iprop->range) {
756                                 fprintf(f, "prop_clamp_min, prop_clamp_max);");
757                         }
758                         else {
759                                 rna_int_print(f, iprop->hardmin); fprintf(f, ", ");
760                                 rna_int_print(f, iprop->hardmax); fprintf(f, ");\n");
761                         }
762                         return;
763                 }
764         }
765         else if (prop->type == PROP_FLOAT) {
766                 FloatPropertyRNA *fprop = (FloatPropertyRNA *)prop;
767
768                 if (fprop->hardmin != -FLT_MAX || fprop->hardmax != FLT_MAX || fprop->range) {
769                         if (array) fprintf(f, "CLAMPIS(values[i], ");
770                         else fprintf(f, "CLAMPIS(value, ");
771                         if (fprop->range) {
772                                 fprintf(f, "prop_clamp_min, prop_clamp_max);");
773                         }
774                         else {
775                                 rna_float_print(f, fprop->hardmin); fprintf(f, ", ");
776                                 rna_float_print(f, fprop->hardmax); fprintf(f, ");\n");
777                         }
778                         return;
779                 }
780         }
781
782         if (array)
783                 fprintf(f, "values[i];\n");
784         else
785                 fprintf(f, "value;\n");
786 }
787
788 static char *rna_def_property_set_func(FILE *f, StructRNA *srna, PropertyRNA *prop, PropertyDefRNA *dp,
789                                        const char *manualfunc)
790 {
791         char *func;
792
793         if (!(prop->flag & PROP_EDITABLE))
794                 return NULL;
795         if (prop->flag & PROP_IDPROPERTY && manualfunc == NULL)
796                 return NULL;
797
798         if (!manualfunc) {
799                 if (!dp->dnastructname || !dp->dnaname) {
800                         if (prop->flag & PROP_EDITABLE) {
801                                 fprintf(stderr, "%s: %s.%s has no valid dna info.\n",
802                                         __func__, srna->identifier, prop->identifier);
803                                 DefRNA.error = 1;
804                         }
805                         return NULL;
806                 }
807         }
808
809         func = rna_alloc_function_name(srna->identifier, rna_safe_id(prop->identifier), "set");
810
811         switch (prop->type) {
812                 case PROP_STRING:
813                 {
814                         StringPropertyRNA *sprop = (StringPropertyRNA *)prop;
815                         fprintf(f, "void %s(PointerRNA *ptr, const char *value)\n", func);
816                         fprintf(f, "{\n");
817                         if (manualfunc) {
818                                 fprintf(f, "    %s(ptr, value);\n", manualfunc);
819                         }
820                         else {
821                                 const PropertySubType subtype = prop->subtype;
822                                 const char *string_copy_func = (subtype == PROP_FILEPATH ||
823                                                                 subtype == PROP_DIRPATH  ||
824                                                                 subtype == PROP_FILENAME ||
825                                                                 subtype == PROP_BYTESTRING) ?
826                                                                "BLI_strncpy" : "BLI_strncpy_utf8";
827
828                                 rna_print_data_get(f, dp);
829
830                                 if (!(prop->flag & PROP_NEVER_NULL)) {
831                                         fprintf(f, "    if (data->%s == NULL) {\n", dp->dnaname);
832                                         fprintf(f, "            return;\n");
833                                         fprintf(f, "    }\n");
834                                 }
835
836                                 if (sprop->maxlength)
837                                         fprintf(f, "    %s(data->%s, value, %d);\n", string_copy_func, dp->dnaname, sprop->maxlength);
838                                 else
839                                         fprintf(f, "    %s(data->%s, value, sizeof(data->%s));\n", string_copy_func,
840                                                 dp->dnaname, dp->dnaname);
841                         }
842                         fprintf(f, "}\n\n");
843                         break;
844                 }
845                 case PROP_POINTER:
846                 {
847                         fprintf(f, "void %s(PointerRNA *ptr, PointerRNA value)\n", func);
848                         fprintf(f, "{\n");
849                         if (manualfunc) {
850                                 fprintf(f, "    %s(ptr, value);\n", manualfunc);
851                         }
852                         else {
853                                 rna_print_data_get(f, dp);
854
855                                 if (prop->flag & PROP_ID_SELF_CHECK) {
856                                         rna_print_id_get(f, dp);
857                                         fprintf(f, "    if (id == value.data) return;\n\n");
858                                 }
859
860                                 if (prop->flag & PROP_ID_REFCOUNT) {
861                                         fprintf(f, "\n  if (data->%s)\n", dp->dnaname);
862                                         fprintf(f, "            id_us_min((ID *)data->%s);\n", dp->dnaname);
863                                         fprintf(f, "    if (value.data)\n");
864                                         fprintf(f, "            id_us_plus((ID *)value.data);\n\n");
865                                 }
866                                 else {
867                                         PointerPropertyRNA *pprop = (PointerPropertyRNA *)dp->prop;
868                                         StructRNA *type = rna_find_struct((const char *)pprop->type);
869                                         if (type && (type->flag & STRUCT_ID)) {
870                                                 fprintf(f, "    if (value.data)\n");
871                                                 fprintf(f, "            id_lib_extern((ID *)value.data);\n\n");
872                                         }
873                                 }
874
875                                 fprintf(f, "    data->%s = value.data;\n", dp->dnaname);
876
877                         }
878                         fprintf(f, "}\n\n");
879                         break;
880                 }
881                 default:
882                         if (prop->arraydimension) {
883                                 if (prop->flag & PROP_DYNAMIC)
884                                         fprintf(f, "void %s(PointerRNA *ptr, const %s values[])\n", func, rna_type_type(prop));
885                                 else
886                                         fprintf(f, "void %s(PointerRNA *ptr, const %s values[%u])\n", func,
887                                                 rna_type_type(prop), prop->totarraylength);
888                                 fprintf(f, "{\n");
889
890                                 if (manualfunc) {
891                                         fprintf(f, "    %s(ptr, values);\n", manualfunc);
892                                 }
893                                 else {
894                                         rna_print_data_get(f, dp);
895
896                                         if (prop->flag & PROP_DYNAMIC) {
897                                                 char *lenfunc = rna_alloc_function_name(srna->identifier, rna_safe_id(prop->identifier),
898                                                                                         "set_length");
899                                                 fprintf(f, "    int i, arraylen[RNA_MAX_ARRAY_DIMENSION];\n");
900                                                 fprintf(f, "    int len = %s(ptr, arraylen);\n\n", lenfunc);
901                                                 rna_clamp_value_range(f, prop);
902                                                 fprintf(f, "    for (i = 0; i < len; i++) {\n");
903                                                 MEM_freeN(lenfunc);
904                                         }
905                                         else {
906                                                 fprintf(f, "    int i;\n\n");
907                                                 rna_clamp_value_range(f, prop);
908                                                 fprintf(f, "    for (i = 0; i < %u; i++) {\n", prop->totarraylength);
909                                         }
910
911                                         if (dp->dnaarraylength == 1) {
912                                                 if (prop->type == PROP_BOOLEAN && dp->booleanbit) {
913                                                         fprintf(f, "            if (%svalues[i]) data->%s |= (%d<<i);\n",
914                                                                 (dp->booleannegative) ? "!" : "", dp->dnaname, dp->booleanbit);
915                                                         fprintf(f, "            else data->%s &= ~(%d << i);\n", dp->dnaname, dp->booleanbit);
916                                                 }
917                                                 else {
918                                                         fprintf(f, "            (&data->%s)[i] = %s", dp->dnaname, (dp->booleannegative) ? "!" : "");
919                                                         rna_clamp_value(f, prop, 1);
920                                                 }
921                                         }
922                                         else {
923                                                 if (prop->type == PROP_BOOLEAN && dp->booleanbit) {
924                                                         fprintf(f, "            if (%svalues[i]) data->%s[i] |= ", (dp->booleannegative) ? "!" : "",
925                                                                 dp->dnaname);
926                                                         rna_int_print(f, dp->booleanbit);
927                                                         fprintf(f, ";\n");
928                                                         fprintf(f, "            else data->%s[i] &= ~", dp->dnaname);
929                                                         rna_int_print(f, dp->booleanbit);
930                                                         fprintf(f, ";\n");
931                                                 }
932                                                 else if (rna_color_quantize(prop, dp)) {
933                                                         fprintf(f, "            data->%s[i] = FTOCHAR(values[i]);\n", dp->dnaname);
934                                                 }
935                                                 else {
936                                                         if (dp->dnatype)
937                                                                 fprintf(f, "            ((%s *)data->%s)[i] = %s", dp->dnatype, dp->dnaname,
938                                                                         (dp->booleannegative) ? "!" : "");
939                                                         else
940                                                                 fprintf(f, "            (data->%s)[i] = %s", dp->dnaname, (dp->booleannegative) ? "!" : "");
941                                                         rna_clamp_value(f, prop, 1);
942                                                 }
943                                         }
944                                         fprintf(f, "    }\n");
945                                 }
946                                 fprintf(f, "}\n\n");
947                         }
948                         else {
949                                 fprintf(f, "void %s(PointerRNA *ptr, %s value)\n", func, rna_type_type(prop));
950                                 fprintf(f, "{\n");
951
952                                 if (manualfunc) {
953                                         fprintf(f, "    %s(ptr, value);\n", manualfunc);
954                                 }
955                                 else {
956                                         rna_print_data_get(f, dp);
957                                         if (prop->type == PROP_BOOLEAN && dp->booleanbit) {
958                                                 fprintf(f, "    if (%svalue) data->%s |= ", (dp->booleannegative) ? "!" : "", dp->dnaname);
959                                                 rna_int_print(f, dp->booleanbit);
960                                                 fprintf(f, ";\n");
961                                                 fprintf(f, "    else data->%s &= ~", dp->dnaname);
962                                                 rna_int_print(f, dp->booleanbit);
963                                                 fprintf(f, ";\n");
964                                         }
965                                         else if (prop->type == PROP_ENUM && dp->enumbitflags) {
966                                                 fprintf(f, "    data->%s &= ~", dp->dnaname);
967                                                 rna_int_print(f, rna_enum_bitmask(prop));
968                                                 fprintf(f, ";\n");
969                                                 fprintf(f, "    data->%s |= value;\n", dp->dnaname);
970                                         }
971                                         else {
972                                                 rna_clamp_value_range(f, prop);
973                                                 fprintf(f, "    data->%s = %s", dp->dnaname, (dp->booleannegative) ? "!" : "");
974                                                 rna_clamp_value(f, prop, 0);
975                                         }
976                                 }
977                                 fprintf(f, "}\n\n");
978                         }
979                         break;
980         }
981
982         return func;
983 }
984
985 static char *rna_def_property_length_func(FILE *f, StructRNA *srna, PropertyRNA *prop, PropertyDefRNA *dp,
986                                           const char *manualfunc)
987 {
988         char *func = NULL;
989
990         if (prop->flag & PROP_IDPROPERTY && manualfunc == NULL)
991                 return NULL;
992
993         if (prop->type == PROP_STRING) {
994                 if (!manualfunc) {
995                         if (!dp->dnastructname || !dp->dnaname) {
996                                 fprintf(stderr, "%s: %s.%s has no valid dna info.\n",
997                                         __func__, srna->identifier, prop->identifier);
998                                 DefRNA.error = 1;
999                                 return NULL;
1000                         }
1001                 }
1002
1003                 func = rna_alloc_function_name(srna->identifier, rna_safe_id(prop->identifier), "length");
1004
1005                 fprintf(f, "int %s(PointerRNA *ptr)\n", func);
1006                 fprintf(f, "{\n");
1007                 if (manualfunc) {
1008                         fprintf(f, "    return %s(ptr);\n", manualfunc);
1009                 }
1010                 else {
1011                         rna_print_data_get(f, dp);
1012                         if (!(prop->flag & PROP_NEVER_NULL)) {
1013                                 fprintf(f, "    if (data->%s == NULL) return 0;\n", dp->dnaname);
1014                         }
1015                         fprintf(f, "    return strlen(data->%s);\n", dp->dnaname);
1016                 }
1017                 fprintf(f, "}\n\n");
1018         }
1019         else if (prop->type == PROP_COLLECTION) {
1020                 if (!manualfunc) {
1021                         if (prop->type == PROP_COLLECTION && (!(dp->dnalengthname || dp->dnalengthfixed) || !dp->dnaname)) {
1022                                 fprintf(stderr, "%s: %s.%s has no valid dna info.\n",
1023                                         __func__, srna->identifier, prop->identifier);
1024                                 DefRNA.error = 1;
1025                                 return NULL;
1026                         }
1027                 }
1028
1029                 func = rna_alloc_function_name(srna->identifier, rna_safe_id(prop->identifier), "length");
1030
1031                 fprintf(f, "int %s(PointerRNA *ptr)\n", func);
1032                 fprintf(f, "{\n");
1033                 if (manualfunc) {
1034                         fprintf(f, "    return %s(ptr);\n", manualfunc);
1035                 }
1036                 else {
1037                         rna_print_data_get(f, dp);
1038                         if (dp->dnalengthname)
1039                                 fprintf(f, "    return (data->%s == NULL) ? 0 : data->%s;\n", dp->dnaname, dp->dnalengthname);
1040                         else
1041                                 fprintf(f, "    return (data->%s == NULL) ? 0 : %d;\n", dp->dnaname, dp->dnalengthfixed);
1042                 }
1043                 fprintf(f, "}\n\n");
1044         }
1045
1046         return func;
1047 }
1048
1049 static char *rna_def_property_begin_func(FILE *f, StructRNA *srna, PropertyRNA *prop, PropertyDefRNA *dp,
1050                                          const char *manualfunc)
1051 {
1052         char *func, *getfunc;
1053
1054         if (prop->flag & PROP_IDPROPERTY && manualfunc == NULL)
1055                 return NULL;
1056
1057         if (!manualfunc) {
1058                 if (!dp->dnastructname || !dp->dnaname) {
1059                         fprintf(stderr, "%s: %s.%s has no valid dna info.\n",
1060                                 __func__, srna->identifier, prop->identifier);
1061                         DefRNA.error = 1;
1062                         return NULL;
1063                 }
1064         }
1065
1066         func = rna_alloc_function_name(srna->identifier, rna_safe_id(prop->identifier), "begin");
1067
1068         fprintf(f, "void %s(CollectionPropertyIterator *iter, PointerRNA *ptr)\n", func);
1069         fprintf(f, "{\n");
1070
1071         if (!manualfunc)
1072                 rna_print_data_get(f, dp);
1073
1074         fprintf(f, "\n  memset(iter, 0, sizeof(*iter));\n");
1075         fprintf(f, "    iter->parent = *ptr;\n");
1076         fprintf(f, "    iter->prop = (PropertyRNA *)&rna_%s_%s;\n", srna->identifier, prop->identifier);
1077
1078         if (dp->dnalengthname || dp->dnalengthfixed) {
1079                 if (manualfunc) {
1080                         fprintf(f, "\n  %s(iter, ptr);\n", manualfunc);
1081                 }
1082                 else {
1083                         if (dp->dnalengthname)
1084                                 fprintf(f, "\n  rna_iterator_array_begin(iter, data->%s, sizeof(data->%s[0]), data->%s, 0, NULL);\n",
1085                                         dp->dnaname, dp->dnaname, dp->dnalengthname);
1086                         else
1087                                 fprintf(f, "\n  rna_iterator_array_begin(iter, data->%s, sizeof(data->%s[0]), %d, 0, NULL);\n",
1088                                         dp->dnaname, dp->dnaname, dp->dnalengthfixed);
1089                 }
1090         }
1091         else {
1092                 if (manualfunc)
1093                         fprintf(f, "\n  %s(iter, ptr);\n", manualfunc);
1094                 else if (dp->dnapointerlevel == 0)
1095                         fprintf(f, "\n  rna_iterator_listbase_begin(iter, &data->%s, NULL);\n", dp->dnaname);
1096                 else
1097                         fprintf(f, "\n  rna_iterator_listbase_begin(iter, data->%s, NULL);\n", dp->dnaname);
1098         }
1099
1100         getfunc = rna_alloc_function_name(srna->identifier, rna_safe_id(prop->identifier), "get");
1101
1102         fprintf(f, "\n  if (iter->valid)\n");
1103         fprintf(f, "            iter->ptr = %s(iter);\n", getfunc);
1104
1105         fprintf(f, "}\n\n");
1106
1107
1108         return func;
1109 }
1110
1111 static char *rna_def_property_lookup_int_func(FILE *f, StructRNA *srna, PropertyRNA *prop, PropertyDefRNA *dp,
1112                                               const char *manualfunc, const char *nextfunc)
1113 {
1114         /* note on indices, this is for external functions and ignores skipped values.
1115          * so the the index can only be checked against the length when there is no 'skip' function. */
1116         char *func;
1117
1118         if (prop->flag & PROP_IDPROPERTY && manualfunc == NULL)
1119                 return NULL;
1120
1121         if (!manualfunc) {
1122                 if (!dp->dnastructname || !dp->dnaname)
1123                         return NULL;
1124
1125                 /* only supported in case of standard next functions */
1126                 if (strcmp(nextfunc, "rna_iterator_array_next") == 0) {}
1127                 else if (strcmp(nextfunc, "rna_iterator_listbase_next") == 0) {}
1128                 else return NULL;
1129         }
1130
1131         func = rna_alloc_function_name(srna->identifier, rna_safe_id(prop->identifier), "lookup_int");
1132
1133         fprintf(f, "int %s(PointerRNA *ptr, int index, PointerRNA *r_ptr)\n", func);
1134         fprintf(f, "{\n");
1135
1136         if (manualfunc) {
1137                 fprintf(f, "\n  return %s(ptr, index, r_ptr);\n", manualfunc);
1138                 fprintf(f, "}\n\n");
1139                 return func;
1140         }
1141
1142         fprintf(f, "    int found = 0;\n");
1143         fprintf(f, "    CollectionPropertyIterator iter;\n\n");
1144
1145         fprintf(f, "    %s_%s_begin(&iter, ptr);\n\n", srna->identifier, rna_safe_id(prop->identifier));
1146         fprintf(f, "    if (iter.valid) {\n");
1147
1148         if (strcmp(nextfunc, "rna_iterator_array_next") == 0) {
1149                 fprintf(f, "            ArrayIterator *internal = iter.internal;\n");
1150                 fprintf(f, "            if (index < 0 || index >= internal->length) {\n");
1151                 fprintf(f, "#ifdef __GNUC__\n");
1152                 fprintf(f, "                    printf(\"Array iterator out of range: %%s (index %%d)\\n\", __func__, index);\n");
1153                 fprintf(f, "#else\n");
1154                 fprintf(f, "                    printf(\"Array iterator out of range: (index %%d)\\n\", index);\n");
1155                 fprintf(f, "#endif\n");
1156                 fprintf(f, "            }\n");
1157                 fprintf(f, "            else if (internal->skip) {\n");
1158                 fprintf(f, "                    while (index-- > 0 && iter.valid) {\n");
1159                 fprintf(f, "                            rna_iterator_array_next(&iter);\n");
1160                 fprintf(f, "                    }\n");
1161                 fprintf(f, "                    found = (index == -1 && iter.valid);\n");
1162                 fprintf(f, "            }\n");
1163                 fprintf(f, "            else {\n");
1164                 fprintf(f, "                    internal->ptr += internal->itemsize * index;\n");
1165                 fprintf(f, "                    found = 1;\n");
1166                 fprintf(f, "            }\n");
1167         }
1168         else if (strcmp(nextfunc, "rna_iterator_listbase_next") == 0) {
1169                 fprintf(f, "            ListBaseIterator *internal = iter.internal;\n");
1170                 fprintf(f, "            if (internal->skip) {\n");
1171                 fprintf(f, "                    while (index-- > 0 && iter.valid) {\n");
1172                 fprintf(f, "                            rna_iterator_listbase_next(&iter);\n");
1173                 fprintf(f, "                    }\n");
1174                 fprintf(f, "                    found = (index == -1 && iter.valid);\n");
1175                 fprintf(f, "            }\n");
1176                 fprintf(f, "            else {\n");
1177                 fprintf(f, "                    while (index-- > 0 && internal->link)\n");
1178                 fprintf(f, "                            internal->link = internal->link->next;\n");
1179                 fprintf(f, "                    found = (index == -1 && internal->link);\n");
1180                 fprintf(f, "            }\n");
1181         }
1182
1183         fprintf(f, "            if (found) *r_ptr = %s_%s_get(&iter);\n", srna->identifier, rna_safe_id(prop->identifier));
1184         fprintf(f, "    }\n\n");
1185         fprintf(f, "    %s_%s_end(&iter);\n\n", srna->identifier, rna_safe_id(prop->identifier));
1186
1187         fprintf(f, "    return found;\n");
1188
1189 #if 0
1190         rna_print_data_get(f, dp);
1191         item_type = (cprop->item_type) ? (const char *)cprop->item_type : "UnknownType";
1192
1193         if (dp->dnalengthname || dp->dnalengthfixed) {
1194                 if (dp->dnalengthname)
1195                         fprintf(f, "\n  rna_array_lookup_int(ptr, &RNA_%s, data->%s, sizeof(data->%s[0]), data->%s, index);\n",
1196                                 item_type, dp->dnaname, dp->dnaname, dp->dnalengthname);
1197                 else
1198                         fprintf(f, "\n  rna_array_lookup_int(ptr, &RNA_%s, data->%s, sizeof(data->%s[0]), %d, index);\n",
1199                                 item_type, dp->dnaname, dp->dnaname, dp->dnalengthfixed);
1200         }
1201         else {
1202                 if (dp->dnapointerlevel == 0)
1203                         fprintf(f, "\n  return rna_listbase_lookup_int(ptr, &RNA_%s, &data->%s, index);\n",
1204                                 item_type, dp->dnaname);
1205                 else
1206                         fprintf(f, "\n  return rna_listbase_lookup_int(ptr, &RNA_%s, data->%s, index);\n", item_type, dp->dnaname);
1207         }
1208 #endif
1209
1210         fprintf(f, "}\n\n");
1211
1212         return func;
1213 }
1214
1215 static char *rna_def_property_lookup_string_func(FILE *f, StructRNA *srna, PropertyRNA *prop, PropertyDefRNA *dp,
1216                                                  const char *manualfunc, const char *item_type)
1217 {
1218         char *func;
1219         StructRNA *item_srna, *item_name_base;
1220         PropertyRNA *item_name_prop;
1221         const int namebuflen = 1024;
1222
1223         if (prop->flag & PROP_IDPROPERTY && manualfunc == NULL)
1224                 return NULL;
1225
1226         if (!manualfunc) {
1227                 if (!dp->dnastructname || !dp->dnaname)
1228                         return NULL;
1229
1230                 /* only supported for collection items with name properties */
1231                 item_srna = rna_find_struct(item_type);
1232                 if (item_srna && item_srna->nameproperty) {
1233                         item_name_prop = item_srna->nameproperty;
1234                         item_name_base = item_srna;
1235                         while (item_name_base->base && item_name_base->base->nameproperty == item_name_prop)
1236                                 item_name_base = item_name_base->base;
1237                 }
1238                 else
1239                         return NULL;
1240         }
1241
1242         func = rna_alloc_function_name(srna->identifier, rna_safe_id(prop->identifier), "lookup_string");
1243
1244         fprintf(f, "int %s(PointerRNA *ptr, const char *key, PointerRNA *r_ptr)\n", func);
1245         fprintf(f, "{\n");
1246
1247         if (manualfunc) {
1248                 fprintf(f, "    return %s(ptr, key, r_ptr);\n", manualfunc);
1249                 fprintf(f, "}\n\n");
1250                 return func;
1251         }
1252
1253         /* XXX extern declaration could be avoid by including RNA_blender.h, but this has lots of unknown
1254          * DNA types in functions, leading to conflicting function signatures.
1255          */
1256         fprintf(f, "    extern int %s_%s_length(PointerRNA *);\n", item_name_base->identifier, rna_safe_id(item_name_prop->identifier));
1257         fprintf(f, "    extern void %s_%s_get(PointerRNA *, char *);\n\n", item_name_base->identifier, rna_safe_id(item_name_prop->identifier));
1258
1259         fprintf(f, "    bool found = false;\n");
1260         fprintf(f, "    CollectionPropertyIterator iter;\n");
1261         fprintf(f, "    char namebuf[%d];\n", namebuflen);
1262         fprintf(f, "    char *name;\n\n");
1263
1264         fprintf(f, "    %s_%s_begin(&iter, ptr);\n\n", srna->identifier, rna_safe_id(prop->identifier));
1265
1266         fprintf(f, "    while (iter.valid) {\n");
1267         fprintf(f, "            if (iter.ptr.data) {\n");
1268         fprintf(f, "                    int namelen = %s_%s_length(&iter.ptr);\n", item_name_base->identifier, rna_safe_id(item_name_prop->identifier));
1269         fprintf(f, "                    if (namelen < %d) {\n", namebuflen);
1270         fprintf(f, "                            %s_%s_get(&iter.ptr, namebuf);\n", item_name_base->identifier, rna_safe_id(item_name_prop->identifier));
1271         fprintf(f, "                            if (strcmp(namebuf, key) == 0) {\n");
1272         fprintf(f, "                                    found = true;\n");
1273         fprintf(f, "                                    *r_ptr = iter.ptr;\n");
1274         fprintf(f, "                                    break;\n");
1275         fprintf(f, "                            }\n");
1276         fprintf(f, "                    }\n");
1277         fprintf(f, "                    else {\n");
1278         fprintf(f, "                            name = MEM_mallocN(namelen+1, \"name string\");\n");
1279         fprintf(f, "                            %s_%s_get(&iter.ptr, name);\n", item_name_base->identifier, rna_safe_id(item_name_prop->identifier));
1280         fprintf(f, "                            if (strcmp(name, key) == 0) {\n");
1281         fprintf(f, "                                    MEM_freeN(name);\n\n");
1282         fprintf(f, "                                    found = true;\n");
1283         fprintf(f, "                                    *r_ptr = iter.ptr;\n");
1284         fprintf(f, "                                    break;\n");
1285         fprintf(f, "                            }\n");
1286         fprintf(f, "                            else {\n");
1287         fprintf(f, "                                    MEM_freeN(name);\n");
1288         fprintf(f, "                            }\n");
1289         fprintf(f, "                    }\n");
1290         fprintf(f, "            }\n");
1291         fprintf(f, "            %s_%s_next(&iter);\n", srna->identifier, rna_safe_id(prop->identifier));
1292         fprintf(f, "    }\n");
1293         fprintf(f, "    %s_%s_end(&iter);\n\n", srna->identifier, rna_safe_id(prop->identifier));
1294
1295         fprintf(f, "    return found;\n");
1296         fprintf(f, "}\n\n");
1297
1298         return func;
1299 }
1300
1301 static char *rna_def_property_next_func(FILE *f, StructRNA *srna, PropertyRNA *prop, PropertyDefRNA *UNUSED(dp),
1302                                         const char *manualfunc)
1303 {
1304         char *func, *getfunc;
1305
1306         if (prop->flag & PROP_IDPROPERTY && manualfunc == NULL)
1307                 return NULL;
1308
1309         if (!manualfunc)
1310                 return NULL;
1311
1312         func = rna_alloc_function_name(srna->identifier, rna_safe_id(prop->identifier), "next");
1313
1314         fprintf(f, "void %s(CollectionPropertyIterator *iter)\n", func);
1315         fprintf(f, "{\n");
1316         fprintf(f, "    %s(iter);\n", manualfunc);
1317
1318         getfunc = rna_alloc_function_name(srna->identifier, rna_safe_id(prop->identifier), "get");
1319
1320         fprintf(f, "\n  if (iter->valid)\n");
1321         fprintf(f, "            iter->ptr = %s(iter);\n", getfunc);
1322
1323         fprintf(f, "}\n\n");
1324
1325         return func;
1326 }
1327
1328 static char *rna_def_property_end_func(FILE *f, StructRNA *srna, PropertyRNA *prop, PropertyDefRNA *UNUSED(dp),
1329                                        const char *manualfunc)
1330 {
1331         char *func;
1332
1333         if (prop->flag & PROP_IDPROPERTY && manualfunc == NULL)
1334                 return NULL;
1335
1336         func = rna_alloc_function_name(srna->identifier, rna_safe_id(prop->identifier), "end");
1337
1338         fprintf(f, "void %s(CollectionPropertyIterator *iter)\n", func);
1339         fprintf(f, "{\n");
1340         if (manualfunc)
1341                 fprintf(f, "    %s(iter);\n", manualfunc);
1342         fprintf(f, "}\n\n");
1343
1344         return func;
1345 }
1346
1347 static void rna_set_raw_property(PropertyDefRNA *dp, PropertyRNA *prop)
1348 {
1349         if (dp->dnapointerlevel != 0)
1350                 return;
1351         if (!dp->dnatype || !dp->dnaname || !dp->dnastructname)
1352                 return;
1353         
1354         if (strcmp(dp->dnatype, "char") == 0) {
1355                 prop->rawtype = PROP_RAW_CHAR;
1356                 prop->flag |= PROP_RAW_ACCESS;
1357         }
1358         else if (strcmp(dp->dnatype, "short") == 0) {
1359                 prop->rawtype = PROP_RAW_SHORT;
1360                 prop->flag |= PROP_RAW_ACCESS;
1361         }
1362         else if (strcmp(dp->dnatype, "int") == 0) {
1363                 prop->rawtype = PROP_RAW_INT;
1364                 prop->flag |= PROP_RAW_ACCESS;
1365         }
1366         else if (strcmp(dp->dnatype, "float") == 0) {
1367                 prop->rawtype = PROP_RAW_FLOAT;
1368                 prop->flag |= PROP_RAW_ACCESS;
1369         }
1370         else if (strcmp(dp->dnatype, "double") == 0) {
1371                 prop->rawtype = PROP_RAW_DOUBLE;
1372                 prop->flag |= PROP_RAW_ACCESS;
1373         }
1374 }
1375
1376 static void rna_set_raw_offset(FILE *f, StructRNA *srna, PropertyRNA *prop)
1377 {
1378         PropertyDefRNA *dp = rna_find_struct_property_def(srna, prop);
1379
1380         fprintf(f, "\toffsetof(%s, %s), %d", dp->dnastructname, dp->dnaname, prop->rawtype);
1381 }
1382
1383 static void rna_def_property_funcs(FILE *f, StructRNA *srna, PropertyDefRNA *dp)
1384 {
1385         PropertyRNA *prop;
1386
1387         prop = dp->prop;
1388
1389         switch (prop->type) {
1390                 case PROP_BOOLEAN:
1391                 {
1392                         BoolPropertyRNA *bprop = (BoolPropertyRNA *)prop;
1393
1394                         if (!prop->arraydimension) {
1395                                 if (!bprop->get && !bprop->set && !dp->booleanbit)
1396                                         rna_set_raw_property(dp, prop);
1397
1398                                 bprop->get = (void *)rna_def_property_get_func(f, srna, prop, dp, (const char *)bprop->get);
1399                                 bprop->set = (void *)rna_def_property_set_func(f, srna, prop, dp, (const char *)bprop->set);
1400                         }
1401                         else {
1402                                 bprop->getarray = (void *)rna_def_property_get_func(f, srna, prop, dp, (const char *)bprop->getarray);
1403                                 bprop->setarray = (void *)rna_def_property_set_func(f, srna, prop, dp, (const char *)bprop->setarray);
1404                         }
1405                         break;
1406                 }
1407                 case PROP_INT:
1408                 {
1409                         IntPropertyRNA *iprop = (IntPropertyRNA *)prop;
1410
1411                         if (!prop->arraydimension) {
1412                                 if (!iprop->get && !iprop->set)
1413                                         rna_set_raw_property(dp, prop);
1414
1415                                 iprop->get = (void *)rna_def_property_get_func(f, srna, prop, dp, (const char *)iprop->get);
1416                                 iprop->set = (void *)rna_def_property_set_func(f, srna, prop, dp, (const char *)iprop->set);
1417                         }
1418                         else {
1419                                 if (!iprop->getarray && !iprop->setarray)
1420                                         rna_set_raw_property(dp, prop);
1421
1422                                 iprop->getarray = (void *)rna_def_property_get_func(f, srna, prop, dp, (const char *)iprop->getarray);
1423                                 iprop->setarray = (void *)rna_def_property_set_func(f, srna, prop, dp, (const char *)iprop->setarray);
1424                         }
1425                         break;
1426                 }
1427                 case PROP_FLOAT:
1428                 {
1429                         FloatPropertyRNA *fprop = (FloatPropertyRNA *)prop;
1430
1431                         if (!prop->arraydimension) {
1432                                 if (!fprop->get && !fprop->set)
1433                                         rna_set_raw_property(dp, prop);
1434
1435                                 fprop->get = (void *)rna_def_property_get_func(f, srna, prop, dp, (const char *)fprop->get);
1436                                 fprop->set = (void *)rna_def_property_set_func(f, srna, prop, dp, (const char *)fprop->set);
1437                         }
1438                         else {
1439                                 if (!fprop->getarray && !fprop->setarray)
1440                                         rna_set_raw_property(dp, prop);
1441
1442                                 fprop->getarray = (void *)rna_def_property_get_func(f, srna, prop, dp, (const char *)fprop->getarray);
1443                                 fprop->setarray = (void *)rna_def_property_set_func(f, srna, prop, dp, (const char *)fprop->setarray);
1444                         }
1445                         break;
1446                 }
1447                 case PROP_ENUM:
1448                 {
1449                         EnumPropertyRNA *eprop = (EnumPropertyRNA *)prop;
1450
1451                         eprop->get = (void *)rna_def_property_get_func(f, srna, prop, dp, (const char *)eprop->get);
1452                         eprop->set = (void *)rna_def_property_set_func(f, srna, prop, dp, (const char *)eprop->set);
1453                         break;
1454                 }
1455                 case PROP_STRING:
1456                 {
1457                         StringPropertyRNA *sprop = (StringPropertyRNA *)prop;
1458
1459                         sprop->get = (void *)rna_def_property_get_func(f, srna, prop, dp, (const char *)sprop->get);
1460                         sprop->length = (void *)rna_def_property_length_func(f, srna, prop, dp, (const char *)sprop->length);
1461                         sprop->set = (void *)rna_def_property_set_func(f, srna, prop, dp, (const char *)sprop->set);
1462                         break;
1463                 }
1464                 case PROP_POINTER:
1465                 {
1466                         PointerPropertyRNA *pprop = (PointerPropertyRNA *)prop;
1467
1468                         pprop->get = (void *)rna_def_property_get_func(f, srna, prop, dp, (const char *)pprop->get);
1469                         pprop->set = (void *)rna_def_property_set_func(f, srna, prop, dp, (const char *)pprop->set);
1470                         if (!pprop->type) {
1471                                 fprintf(stderr, "%s: %s.%s, pointer must have a struct type.\n",
1472                                         __func__, srna->identifier, prop->identifier);
1473                                 DefRNA.error = 1;
1474                         }
1475                         break;
1476                 }
1477                 case PROP_COLLECTION:
1478                 {
1479                         CollectionPropertyRNA *cprop = (CollectionPropertyRNA *)prop;
1480                         const char *nextfunc = (const char *)cprop->next;
1481                         const char *item_type = (const char *)cprop->item_type;
1482
1483                         if (dp->dnatype && strcmp(dp->dnatype, "ListBase") == 0) {
1484                                 /* pass */
1485                         }
1486                         else if (dp->dnalengthname || dp->dnalengthfixed) {
1487                                 cprop->length = (void *)rna_def_property_length_func(f, srna, prop, dp, (const char *)cprop->length);
1488                         }
1489
1490                         /* test if we can allow raw array access, if it is using our standard
1491                          * array get/next function, we can be sure it is an actual array */
1492                         if (cprop->next && cprop->get)
1493                                 if (strcmp((const char *)cprop->next, "rna_iterator_array_next") == 0 &&
1494                                     strcmp((const char *)cprop->get, "rna_iterator_array_get") == 0)
1495                                 {
1496                                         prop->flag |= PROP_RAW_ARRAY;
1497                                 }
1498
1499                         cprop->get = (void *)rna_def_property_get_func(f, srna, prop, dp, (const char *)cprop->get);
1500                         cprop->begin = (void *)rna_def_property_begin_func(f, srna, prop, dp, (const char *)cprop->begin);
1501                         cprop->next = (void *)rna_def_property_next_func(f, srna, prop, dp, (const char *)cprop->next);
1502                         cprop->end = (void *)rna_def_property_end_func(f, srna, prop, dp, (const char *)cprop->end);
1503                         cprop->lookupint = (void *)rna_def_property_lookup_int_func(f, srna, prop, dp,
1504                                                                                     (const char *)cprop->lookupint, nextfunc);
1505                         cprop->lookupstring = (void *)rna_def_property_lookup_string_func(f, srna, prop, dp,
1506                                                                                           (const char *)cprop->lookupstring, item_type);
1507
1508                         if (!(prop->flag & PROP_IDPROPERTY)) {
1509                                 if (!cprop->begin) {
1510                                         fprintf(stderr, "%s: %s.%s, collection must have a begin function.\n",
1511                                                 __func__, srna->identifier, prop->identifier);
1512                                         DefRNA.error = 1;
1513                                 }
1514                                 if (!cprop->next) {
1515                                         fprintf(stderr, "%s: %s.%s, collection must have a next function.\n",
1516                                                 __func__, srna->identifier, prop->identifier);
1517                                         DefRNA.error = 1;
1518                                 }
1519                                 if (!cprop->get) {
1520                                         fprintf(stderr, "%s: %s.%s, collection must have a get function.\n",
1521                                                 __func__, srna->identifier, prop->identifier);
1522                                         DefRNA.error = 1;
1523                                 }
1524                         }
1525                         if (!cprop->item_type) {
1526                                 fprintf(stderr, "%s: %s.%s, collection must have a struct type.\n",
1527                                         __func__, srna->identifier, prop->identifier);
1528                                 DefRNA.error = 1;
1529                         }
1530                         break;
1531                 }
1532         }
1533 }
1534
1535 static void rna_def_property_funcs_header(FILE *f, StructRNA *srna, PropertyDefRNA *dp)
1536 {
1537         PropertyRNA *prop;
1538         char *func;
1539
1540         prop = dp->prop;
1541
1542         if (prop->flag & (PROP_IDPROPERTY | PROP_BUILTIN))
1543                 return;
1544
1545         func = rna_alloc_function_name(srna->identifier, rna_safe_id(prop->identifier), "");
1546
1547         switch (prop->type) {
1548                 case PROP_BOOLEAN:
1549                 case PROP_INT:
1550                 {
1551                         if (!prop->arraydimension) {
1552                                 fprintf(f, "int %sget(PointerRNA *ptr);\n", func);
1553                                 fprintf(f, "void %sset(PointerRNA *ptr, int value);\n", func);
1554                         }
1555                         else if (prop->arraydimension && prop->totarraylength) {
1556                                 fprintf(f, "void %sget(PointerRNA *ptr, int values[%u]);\n", func, prop->totarraylength);
1557                                 fprintf(f, "void %sset(PointerRNA *ptr, const int values[%d]);\n", func, prop->totarraylength);
1558                         }
1559                         else {
1560                                 fprintf(f, "void %sget(PointerRNA *ptr, int values[]);\n", func);
1561                                 fprintf(f, "void %sset(PointerRNA *ptr, const int values[]);\n", func);
1562                         }
1563                         break;
1564                 }
1565                 case PROP_FLOAT:
1566                 {
1567                         if (!prop->arraydimension) {
1568                                 fprintf(f, "float %sget(PointerRNA *ptr);\n", func);
1569                                 fprintf(f, "void %sset(PointerRNA *ptr, float value);\n", func);
1570                         }
1571                         else if (prop->arraydimension && prop->totarraylength) {
1572                                 fprintf(f, "void %sget(PointerRNA *ptr, float values[%u]);\n", func, prop->totarraylength);
1573                                 fprintf(f, "void %sset(PointerRNA *ptr, const float values[%d]);\n", func, prop->totarraylength);
1574                         }
1575                         else {
1576                                 fprintf(f, "void %sget(PointerRNA *ptr, float values[]);\n", func);
1577                                 fprintf(f, "void %sset(PointerRNA *ptr, const float values[]);", func);
1578                         }
1579                         break;
1580                 }
1581                 case PROP_ENUM:
1582                 {
1583                         EnumPropertyRNA *eprop = (EnumPropertyRNA *)prop;
1584                         int i;
1585
1586                         if (eprop->item) {
1587                                 fprintf(f, "enum {\n");
1588
1589                                 for (i = 0; i < eprop->totitem; i++)
1590                                         if (eprop->item[i].identifier[0])
1591                                                 fprintf(f, "\t%s_%s_%s = %d,\n", srna->identifier, prop->identifier,
1592                                                         eprop->item[i].identifier, eprop->item[i].value);
1593
1594                                 fprintf(f, "};\n\n");
1595                         }
1596
1597                         fprintf(f, "int %sget(PointerRNA *ptr);\n", func);
1598                         fprintf(f, "void %sset(PointerRNA *ptr, int value);\n", func);
1599
1600                         break;
1601                 }
1602                 case PROP_STRING:
1603                 {
1604                         StringPropertyRNA *sprop = (StringPropertyRNA *)prop;
1605
1606                         if (sprop->maxlength) {
1607                                 fprintf(f, "#define %s_%s_MAX %d\n\n", srna->identifier, prop->identifier, sprop->maxlength);
1608                         }
1609                         
1610                         fprintf(f, "void %sget(PointerRNA *ptr, char *value);\n", func);
1611                         fprintf(f, "int %slength(PointerRNA *ptr);\n", func);
1612                         fprintf(f, "void %sset(PointerRNA *ptr, const char *value);\n", func);
1613
1614                         break;
1615                 }
1616                 case PROP_POINTER:
1617                 {
1618                         fprintf(f, "PointerRNA %sget(PointerRNA *ptr);\n", func);
1619                         /*fprintf(f, "void %sset(PointerRNA *ptr, PointerRNA value);\n", func); */
1620                         break;
1621                 }
1622                 case PROP_COLLECTION:
1623                 {
1624                         CollectionPropertyRNA *cprop = (CollectionPropertyRNA *)prop;
1625                         fprintf(f, "void %sbegin(CollectionPropertyIterator *iter, PointerRNA *ptr);\n", func);
1626                         fprintf(f, "void %snext(CollectionPropertyIterator *iter);\n", func);
1627                         fprintf(f, "void %send(CollectionPropertyIterator *iter);\n", func);
1628                         if (cprop->length)
1629                                 fprintf(f, "int %slength(PointerRNA *ptr);\n", func);
1630                         if (cprop->lookupint)
1631                                 fprintf(f, "int %slookup_int(PointerRNA *ptr, int key, PointerRNA *r_ptr);\n", func);
1632                         if (cprop->lookupstring)
1633                                 fprintf(f, "int %slookup_string(PointerRNA *ptr, const char *key, PointerRNA *r_ptr);\n", func);
1634                         break;
1635                 }
1636         }
1637
1638         if (prop->getlength) {
1639                 char funcname[2048];
1640                 rna_construct_wrapper_function_name(funcname, sizeof(funcname), srna->identifier, prop->identifier, "get_length");
1641                 fprintf(f, "int %s(PointerRNA *ptr, int *arraylen);\n", funcname);
1642         }
1643
1644         fprintf(f, "\n");
1645 }
1646
1647 static void rna_def_function_funcs_header(FILE *f, StructRNA *srna, FunctionDefRNA *dfunc)
1648 {
1649         FunctionRNA *func = dfunc->func;
1650         char funcname[2048];
1651
1652         rna_construct_wrapper_function_name(funcname, sizeof(funcname), srna->identifier, func->identifier, NULL);
1653         rna_generate_static_parameter_prototypes(f, srna, dfunc, funcname, 1);
1654 }
1655
1656 static void rna_def_property_funcs_header_cpp(FILE *f, StructRNA *srna, PropertyDefRNA *dp)
1657 {
1658         PropertyRNA *prop;
1659
1660         prop = dp->prop;
1661
1662         if (prop->flag & (PROP_IDPROPERTY | PROP_BUILTIN))
1663                 return;
1664         
1665         /* disabled for now to avoid msvc compiler error due to large file size */
1666 #if 0
1667         if (prop->name && prop->description && prop->description[0] != '\0')
1668                 fprintf(f, "\t/* %s: %s */\n", prop->name, prop->description);
1669         else if (prop->name)
1670                 fprintf(f, "\t/* %s */\n", prop->name);
1671         else
1672                 fprintf(f, "\t/* */\n");
1673 #endif
1674
1675         switch (prop->type) {
1676                 case PROP_BOOLEAN:
1677                 {
1678                         if (!prop->arraydimension) {
1679                                 fprintf(f, "\tinline bool %s(void);\n", rna_safe_id(prop->identifier));
1680                                 fprintf(f, "\tinline void %s(int value);", rna_safe_id(prop->identifier));
1681                         }
1682                         else if (prop->totarraylength) {
1683                                 fprintf(f, "\tinline Array<int, %u> %s(void);\n", prop->totarraylength, rna_safe_id(prop->identifier));
1684                                 fprintf(f, "\tinline void %s(int values[%u]);", rna_safe_id(prop->identifier), prop->totarraylength);
1685                         }
1686                         else if (prop->getlength) {
1687                                 fprintf(f, "\tinline DynamicArray<int> %s(void);\n", rna_safe_id(prop->identifier));
1688                                 fprintf(f, "\tinline void %s(int values[]);", rna_safe_id(prop->identifier));
1689                         }
1690                         break;
1691                 }
1692                 case PROP_INT:
1693                 {
1694                         if (!prop->arraydimension) {
1695                                 fprintf(f, "\tinline int %s(void);\n", rna_safe_id(prop->identifier));
1696                                 fprintf(f, "\tinline void %s(int value);", rna_safe_id(prop->identifier));
1697                         }
1698                         else if (prop->totarraylength) {
1699                                 fprintf(f, "\tinline Array<int, %u> %s(void);\n", prop->totarraylength, rna_safe_id(prop->identifier));
1700                                 fprintf(f, "\tinline void %s(int values[%u]);", rna_safe_id(prop->identifier), prop->totarraylength);
1701                         }
1702                         else if (prop->getlength) {
1703                                 fprintf(f, "\tinline DynamicArray<int> %s(void);\n", rna_safe_id(prop->identifier));
1704                                 fprintf(f, "\tinline void %s(int values[]);", rna_safe_id(prop->identifier));
1705                         }
1706                         break;
1707                 }
1708                 case PROP_FLOAT:
1709                 {
1710                         if (!prop->arraydimension) {
1711                                 fprintf(f, "\tinline float %s(void);\n", rna_safe_id(prop->identifier));
1712                                 fprintf(f, "\tinline void %s(float value);", rna_safe_id(prop->identifier));
1713                         }
1714                         else if (prop->totarraylength) {
1715                                 fprintf(f, "\tinline Array<float, %u> %s(void);\n", prop->totarraylength, rna_safe_id(prop->identifier));
1716                                 fprintf(f, "\tinline void %s(float values[%u]);", rna_safe_id(prop->identifier), prop->totarraylength);
1717                         }
1718                         else if (prop->getlength) {
1719                                 fprintf(f, "\tinline DynamicArray<float> %s(void);\n", rna_safe_id(prop->identifier));
1720                                 fprintf(f, "\tinline void %s(float values[]);", rna_safe_id(prop->identifier));
1721                         }
1722                         break;
1723                 }
1724                 case PROP_ENUM:
1725                 {
1726                         EnumPropertyRNA *eprop = (EnumPropertyRNA *)prop;
1727                         int i;
1728
1729                         if (eprop->item) {
1730                                 fprintf(f, "\tenum %s_enum {\n", rna_safe_id(prop->identifier));
1731
1732                                 for (i = 0; i < eprop->totitem; i++)
1733                                         if (eprop->item[i].identifier[0])
1734                                                 fprintf(f, "\t\t%s_%s = %d,\n", rna_safe_id(prop->identifier), eprop->item[i].identifier,
1735                                                         eprop->item[i].value);
1736
1737                                 fprintf(f, "\t};\n");
1738                         }
1739
1740                         fprintf(f, "\tinline %s_enum %s(void);\n", rna_safe_id(prop->identifier), rna_safe_id(prop->identifier));
1741                         fprintf(f, "\tinline void %s(%s_enum value);", rna_safe_id(prop->identifier), rna_safe_id(prop->identifier));
1742                         break;
1743                 }
1744                 case PROP_STRING:
1745                 {
1746                         fprintf(f, "\tinline std::string %s(void);", rna_safe_id(prop->identifier));
1747                         fprintf(f, "\tinline void %s(const std::string& value);", rna_safe_id(prop->identifier));
1748                         break;
1749                 }
1750                 case PROP_POINTER:
1751                 {
1752                         PointerPropertyRNA *pprop = (PointerPropertyRNA *)dp->prop;
1753
1754                         if (pprop->type)
1755                                 fprintf(f, "\tinline %s %s(void);", (const char *)pprop->type, rna_safe_id(prop->identifier));
1756                         else
1757                                 fprintf(f, "\tinline %s %s(void);", "UnknownType", rna_safe_id(prop->identifier));
1758                         break;
1759                 }
1760                 case PROP_COLLECTION:
1761                 {
1762                         CollectionPropertyRNA *cprop = (CollectionPropertyRNA *)dp->prop;
1763                         const char *collection_funcs = "DefaultCollectionFunctions";
1764
1765                         if (!(dp->prop->flag & (PROP_IDPROPERTY | PROP_BUILTIN)) && cprop->property.srna)
1766                                 collection_funcs  = (char *)cprop->property.srna;
1767
1768                         if (cprop->item_type)
1769                                 fprintf(f, "\tCOLLECTION_PROPERTY(%s, %s, %s, %s, %s, %s, %s)", collection_funcs, (const char *)cprop->item_type, srna->identifier,
1770                                         rna_safe_id(prop->identifier), (cprop->length ? "TRUE" : "FALSE"),
1771                                         (cprop->lookupint ? "TRUE" : "FALSE"), (cprop->lookupstring ? "TRUE" : "FALSE"));
1772                         else
1773                                 fprintf(f, "\tCOLLECTION_PROPERTY(%s, %s, %s, %s, %s, %s, %s)", collection_funcs, "UnknownType", srna->identifier,
1774                                         rna_safe_id(prop->identifier), (cprop->length ? "TRUE" : "FALSE"),
1775                                         (cprop->lookupint ? "TRUE" : "FALSE"), (cprop->lookupstring ? "TRUE" : "FALSE"));
1776                         break;
1777                 }
1778         }
1779
1780         fprintf(f, "\n");
1781 }
1782
1783 static const char *rna_parameter_type_cpp_name(PropertyRNA *prop)
1784 {
1785         if (prop->type == PROP_POINTER) {
1786                 /* for cpp api we need to use RNA structures names for pointers */
1787                 PointerPropertyRNA *pprop = (PointerPropertyRNA *) prop;
1788
1789                 return (const char *) pprop->type;
1790         }
1791         else {
1792                 return rna_parameter_type_name(prop);
1793         }
1794 }
1795
1796 static void rna_def_struct_function_prototype_cpp(FILE *f, StructRNA *UNUSED(srna), FunctionDefRNA *dfunc,
1797                                                   const char *namespace, int close_prototype)
1798 {
1799         PropertyDefRNA *dp;
1800         FunctionRNA *func = dfunc->func;
1801
1802         int first = 1;
1803         const char *retval_type = "void";
1804
1805         if (func->c_ret) {
1806                 dp = rna_find_parameter_def(func->c_ret);
1807                 retval_type = rna_parameter_type_cpp_name(dp->prop);
1808         }
1809
1810         if (namespace && namespace[0])
1811                 fprintf(f, "\tinline %s %s::%s(", retval_type, namespace, rna_safe_id(func->identifier));
1812         else
1813                 fprintf(f, "\tinline %s %s(", retval_type, rna_safe_id(func->identifier));
1814
1815         if (func->flag & FUNC_USE_MAIN)
1816                 WRITE_PARAM("void *main");
1817
1818         if (func->flag & FUNC_USE_CONTEXT)
1819                 WRITE_PARAM("Context C");
1820
1821         for (dp = dfunc->cont.properties.first; dp; dp = dp->next) {
1822                 int type, flag, pout;
1823                 const char *ptrstr;
1824
1825                 if (dp->prop == func->c_ret)
1826                         continue;
1827
1828                 type = dp->prop->type;
1829                 flag = dp->prop->flag;
1830                 pout = (flag & PROP_OUTPUT);
1831
1832                 if (flag & PROP_DYNAMIC)
1833                         ptrstr = pout ? "**" : "*";
1834                 else if (type == PROP_POINTER)
1835                         ptrstr = pout ? "*": "";
1836                 else if (dp->prop->arraydimension)
1837                         ptrstr = "*";
1838                 else if (type == PROP_STRING && (flag & PROP_THICK_WRAP))
1839                         ptrstr = "";
1840                 else
1841                         ptrstr = pout ? "*" : "";
1842
1843                 WRITE_COMMA;
1844
1845                 if (flag & PROP_DYNAMIC)
1846                         fprintf(f, "int %s%s_len, ", (flag & PROP_OUTPUT) ? "*" : "", dp->prop->identifier);
1847
1848                 if (!(flag & PROP_DYNAMIC) && dp->prop->arraydimension)
1849                         fprintf(f, "%s %s[%u]", rna_parameter_type_cpp_name(dp->prop),
1850                                 rna_safe_id(dp->prop->identifier), dp->prop->totarraylength);
1851                 else
1852                         fprintf(f, "%s %s%s", rna_parameter_type_cpp_name(dp->prop),
1853                                 ptrstr, rna_safe_id(dp->prop->identifier));
1854         }
1855
1856         fprintf(f, ")");
1857         if (close_prototype)
1858                 fprintf(f, ";\n");
1859 }
1860
1861 static void rna_def_struct_function_header_cpp(FILE *f, StructRNA *srna, FunctionDefRNA *dfunc)
1862 {
1863         if (dfunc->call) {
1864                 /* disabled for now to avoid msvc compiler error due to large file size */
1865 #if 0
1866                 FunctionRNA *func = dfunc->func;
1867                 fprintf(f, "\n\t/* %s */\n", func->description);
1868 #endif
1869
1870                 rna_def_struct_function_prototype_cpp(f, srna, dfunc, NULL, 1);
1871         }
1872 }
1873
1874 static void rna_def_property_funcs_impl_cpp(FILE *f, StructRNA *srna, PropertyDefRNA *dp)
1875 {
1876         PropertyRNA *prop;
1877
1878         prop = dp->prop;
1879
1880         if (prop->flag & (PROP_IDPROPERTY | PROP_BUILTIN))
1881                 return;
1882
1883         switch (prop->type) {
1884                 case PROP_BOOLEAN:
1885                 {
1886                         if (!prop->arraydimension)
1887                                 fprintf(f, "\tBOOLEAN_PROPERTY(%s, %s)", srna->identifier, rna_safe_id(prop->identifier));
1888                         else if (prop->totarraylength)
1889                                 fprintf(f, "\tBOOLEAN_ARRAY_PROPERTY(%s, %u, %s)", srna->identifier, prop->totarraylength,
1890                                         rna_safe_id(prop->identifier));
1891                         else if (prop->getlength)
1892                                 fprintf(f, "\tBOOLEAN_DYNAMIC_ARRAY_PROPERTY(%s, %s)", srna->identifier,
1893                                         rna_safe_id(prop->identifier));
1894                         break;
1895                 }
1896                 case PROP_INT:
1897                 {
1898                         if (!prop->arraydimension)
1899                                 fprintf(f, "\tINT_PROPERTY(%s, %s)", srna->identifier, rna_safe_id(prop->identifier));
1900                         else if (prop->totarraylength)
1901                                 fprintf(f, "\tINT_ARRAY_PROPERTY(%s, %u, %s)", srna->identifier, prop->totarraylength,
1902                                         rna_safe_id(prop->identifier));
1903                         else if (prop->getlength)
1904                                 fprintf(f, "\tINT_DYNAMIC_ARRAY_PROPERTY(%s, %s)", srna->identifier,
1905                                         rna_safe_id(prop->identifier));
1906                         break;
1907                 }
1908                 case PROP_FLOAT:
1909                 {
1910                         if (!prop->arraydimension)
1911                                 fprintf(f, "\tFLOAT_PROPERTY(%s, %s)", srna->identifier, rna_safe_id(prop->identifier));
1912                         else if (prop->totarraylength)
1913                                 fprintf(f, "\tFLOAT_ARRAY_PROPERTY(%s, %u, %s)", srna->identifier, prop->totarraylength,
1914                                         rna_safe_id(prop->identifier));
1915                         else if (prop->getlength)
1916                                 fprintf(f, "\tFLOAT_DYNAMIC_ARRAY_PROPERTY(%s, %s)", srna->identifier,
1917                                         rna_safe_id(prop->identifier));
1918                         break;
1919                 }
1920                 case PROP_ENUM:
1921                 {
1922                         fprintf(f, "\tENUM_PROPERTY(%s_enum, %s, %s)", rna_safe_id(prop->identifier), srna->identifier,
1923                                 rna_safe_id(prop->identifier));
1924
1925                         break;
1926                 }
1927                 case PROP_STRING:
1928                 {
1929                         fprintf(f, "\tSTRING_PROPERTY(%s, %s)", srna->identifier, rna_safe_id(prop->identifier));
1930                         break;
1931                 }
1932                 case PROP_POINTER:
1933                 {
1934                         PointerPropertyRNA *pprop = (PointerPropertyRNA *)dp->prop;
1935
1936                         if (pprop->type)
1937                                 fprintf(f, "\tPOINTER_PROPERTY(%s, %s, %s)", (const char *)pprop->type, srna->identifier,
1938                                         rna_safe_id(prop->identifier));
1939                         else
1940                                 fprintf(f, "\tPOINTER_PROPERTY(%s, %s, %s)", "UnknownType", srna->identifier,
1941                                         rna_safe_id(prop->identifier));
1942                         break;
1943                 }
1944                 case PROP_COLLECTION:
1945                 {
1946 #if 0
1947                         CollectionPropertyRNA *cprop = (CollectionPropertyRNA *)dp->prop;
1948
1949                         if (cprop->type)
1950                                 fprintf(f, "\tCOLLECTION_PROPERTY(%s, %s, %s, %s, %s, %s)", (const char *)cprop->type, srna->identifier,
1951                                         prop->identifier, (cprop->length ? "TRUE" : "FALSE"),
1952                                         (cprop->lookupint ? "TRUE" : "FALSE"), (cprop->lookupstring ? "TRUE" : "FALSE"));
1953                         else
1954                                 fprintf(f, "\tCOLLECTION_PROPERTY(%s, %s, %s, %s, %s, %s)", "UnknownType", srna->identifier,
1955                                         prop->identifier, (cprop->length ? "TRUE" : "FALSE"),
1956                                         (cprop->lookupint ? "TRUE" : "FALSE"), (cprop->lookupstring ? "TRUE" : "FALSE"));
1957 #endif
1958                         break;
1959                 }
1960         }
1961
1962         fprintf(f, "\n");
1963 }
1964
1965 static void rna_def_struct_function_call_impl_cpp(FILE *f, StructRNA *srna, FunctionDefRNA *dfunc)
1966 {
1967         PropertyDefRNA *dp;
1968         StructDefRNA *dsrna;
1969         FunctionRNA *func = dfunc->func;
1970         char funcname[2048];
1971
1972         int first = 1;
1973
1974         rna_construct_wrapper_function_name(funcname, sizeof(funcname), srna->identifier, func->identifier, NULL);
1975
1976         fprintf(f, "%s(", funcname);
1977
1978         dsrna = rna_find_struct_def(srna);
1979
1980         if (func->flag & FUNC_USE_SELF_ID)
1981                 WRITE_PARAM("(::ID *) ptr.id.data");
1982
1983         if ((func->flag & FUNC_NO_SELF) == 0) {
1984                 WRITE_COMMA;
1985                 if (dsrna->dnaname) fprintf(f, "(::%s *) this->ptr.data", dsrna->dnaname);
1986                 else fprintf(f, "(::%s *) this->ptr.data", srna->identifier);
1987         }
1988         else if (func->flag & FUNC_USE_SELF_TYPE) {
1989                 WRITE_COMMA;
1990                 fprintf(f, "this->ptr.type");
1991         }
1992
1993         if (func->flag & FUNC_USE_MAIN)
1994                 WRITE_PARAM("(::Main *) main");
1995
1996         if (func->flag & FUNC_USE_CONTEXT)
1997                 WRITE_PARAM("(::bContext *) C.ptr.data");
1998
1999         if (func->flag & FUNC_USE_REPORTS)
2000                 WRITE_PARAM("NULL");
2001
2002         dp = dfunc->cont.properties.first;
2003         for (; dp; dp = dp->next) {
2004                 if (dp->prop == func->c_ret)
2005                         continue;
2006
2007                 WRITE_COMMA;
2008
2009                 if (dp->prop->flag & PROP_DYNAMIC)
2010                         fprintf(f, "%s_len, ", dp->prop->identifier);
2011
2012                 if (dp->prop->type == PROP_POINTER)
2013                         if ((dp->prop->flag & PROP_RNAPTR) && !(dp->prop->flag & PROP_THICK_WRAP))
2014                                 fprintf(f, "(::%s *) &%s.ptr", rna_parameter_type_name(dp->prop), rna_safe_id(dp->prop->identifier));
2015                         else if (dp->prop->flag & PROP_OUTPUT)
2016                                 fprintf(f, "(::%s **) &%s->ptr.data", rna_parameter_type_name(dp->prop), rna_safe_id(dp->prop->identifier));
2017                         else
2018                                 fprintf(f, "(::%s *) %s.ptr.data", rna_parameter_type_name(dp->prop), rna_safe_id(dp->prop->identifier));
2019                 else
2020                         fprintf(f, "%s", rna_safe_id(dp->prop->identifier));
2021         }
2022
2023         fprintf(f, ");\n");
2024 }
2025
2026 static void rna_def_struct_function_impl_cpp(FILE *f, StructRNA *srna, FunctionDefRNA *dfunc)
2027 {
2028         PropertyDefRNA *dp;
2029         PointerPropertyRNA *pprop;
2030
2031         FunctionRNA *func = dfunc->func;
2032
2033         if (!dfunc->call)
2034                 return;
2035
2036         rna_def_struct_function_prototype_cpp(f, srna, dfunc, srna->identifier, 0);
2037
2038         fprintf(f, " {\n");
2039
2040         if (func->c_ret) {
2041                 dp = rna_find_parameter_def(func->c_ret);
2042
2043                 if (dp->prop->type == PROP_POINTER) {
2044                         pprop = (PointerPropertyRNA *) dp->prop;
2045
2046                         fprintf(f, "\t\tPointerRNA result;\n");
2047
2048                         if ((dp->prop->flag & PROP_RNAPTR) == 0) {
2049                                 StructRNA *ret_srna = rna_find_struct((const char *) pprop->type);
2050                                 fprintf(f, "\t\t::%s *retdata = ", rna_parameter_type_name(dp->prop));
2051                                 rna_def_struct_function_call_impl_cpp(f, srna, dfunc);
2052                                 if (ret_srna->flag & STRUCT_ID)
2053                                         fprintf(f, "\t\tRNA_id_pointer_create((::ID *) retdata, &result);\n");
2054                                 else
2055                                         fprintf(f, "\t\tRNA_pointer_create((::ID *) ptr.id.data, &RNA_%s, retdata, &result);\n", (const char *) pprop->type);
2056                         }
2057                         else {
2058                                 fprintf(f, "\t\tresult = ");
2059                                 rna_def_struct_function_call_impl_cpp(f, srna, dfunc);
2060                         }
2061
2062                         fprintf(f, "\t\treturn %s(result);\n", (const char *) pprop->type);
2063                 }
2064                 else {
2065                         fprintf(f, "\t\treturn ");
2066                         rna_def_struct_function_call_impl_cpp(f, srna, dfunc);
2067                 }
2068         }
2069         else {
2070                 fprintf(f, "\t\t");
2071                 rna_def_struct_function_call_impl_cpp(f, srna, dfunc);
2072         }
2073
2074         fprintf(f, "\t}\n\n");
2075 }
2076
2077 static void rna_def_property_wrapper_funcs(FILE *f, StructDefRNA *dsrna, PropertyDefRNA *dp)
2078 {
2079         if (dp->prop->getlength) {
2080                 char funcname[2048];
2081                 rna_construct_wrapper_function_name(funcname, sizeof(funcname), dsrna->srna->identifier, dp->prop->identifier, "get_length");
2082                 fprintf(f, "int %s(PointerRNA *ptr, int *arraylen)\n", funcname);
2083                 fprintf(f, "{\n");
2084                 fprintf(f, "\treturn %s(ptr, arraylen);\n", rna_function_string(dp->prop->getlength));
2085                 fprintf(f, "}\n\n");
2086         }
2087 }
2088
2089 static void rna_def_function_wrapper_funcs(FILE *f, StructDefRNA *dsrna, FunctionDefRNA *dfunc)
2090 {
2091         StructRNA *srna = dsrna->srna;
2092         FunctionRNA *func = dfunc->func;
2093         PropertyDefRNA *dparm;
2094
2095         int first;
2096         char funcname[2048];
2097
2098         if (!dfunc->call)
2099                 return;
2100
2101         rna_construct_wrapper_function_name(funcname, sizeof(funcname), srna->identifier, func->identifier, NULL);
2102
2103         rna_generate_static_parameter_prototypes(f, srna, dfunc, funcname, 0);
2104
2105         fprintf(f, "\n{\n");
2106
2107         if (func->c_ret)
2108                 fprintf(f, "\treturn %s(", dfunc->call);
2109         else
2110                 fprintf(f, "\t%s(", dfunc->call);
2111
2112         first = 1;
2113
2114         if (func->flag & FUNC_USE_SELF_ID)
2115                 WRITE_PARAM("_selfid");
2116
2117         if ((func->flag & FUNC_NO_SELF) == 0) {
2118                 WRITE_PARAM("_self");
2119         }
2120         else if (func->flag & FUNC_USE_SELF_TYPE) {
2121                 WRITE_PARAM("_type");
2122         }
2123
2124         if (func->flag & FUNC_USE_MAIN)
2125                 WRITE_PARAM("bmain");
2126
2127         if (func->flag & FUNC_USE_CONTEXT)
2128                 WRITE_PARAM("C");
2129
2130         if (func->flag & FUNC_USE_REPORTS)
2131                 WRITE_PARAM("reports");
2132
2133         dparm = dfunc->cont.properties.first;
2134         for (; dparm; dparm = dparm->next) {
2135                 if (dparm->prop == func->c_ret)
2136                         continue;
2137
2138                 WRITE_COMMA;
2139
2140                 if (dparm->prop->flag & PROP_DYNAMIC)
2141                         fprintf(f, "%s_len, %s", dparm->prop->identifier, dparm->prop->identifier);
2142                 else
2143                         fprintf(f, "%s", rna_safe_id(dparm->prop->identifier));
2144         }
2145
2146         fprintf(f, ");\n");
2147         fprintf(f, "}\n\n");
2148 }
2149
2150 static void rna_def_function_funcs(FILE *f, StructDefRNA *dsrna, FunctionDefRNA *dfunc)
2151 {
2152         StructRNA *srna;
2153         FunctionRNA *func;
2154         PropertyDefRNA *dparm;
2155         PropertyType type;
2156         const char *funcname, *valstr;
2157         const char *ptrstr;
2158         const short has_data = (dfunc->cont.properties.first != NULL);
2159         int flag, pout, cptr, first;
2160
2161         srna = dsrna->srna;
2162         func = dfunc->func;
2163
2164         if (!dfunc->call)
2165                 return;
2166
2167         funcname = rna_alloc_function_name(srna->identifier, func->identifier, "call");
2168
2169         /* function definition */
2170         fprintf(f, "void %s(bContext *C, ReportList *reports, PointerRNA *_ptr, ParameterList *_parms)", funcname);
2171         fprintf(f, "\n{\n");
2172
2173         /* variable definitions */
2174         
2175         if (func->flag & FUNC_USE_SELF_ID) {
2176                 fprintf(f, "\tstruct ID *_selfid;\n");
2177         }
2178
2179         if ((func->flag & FUNC_NO_SELF) == 0) {
2180                 if (dsrna->dnaname) fprintf(f, "\tstruct %s *_self;\n", dsrna->dnaname);
2181                 else fprintf(f, "\tstruct %s *_self;\n", srna->identifier);
2182         }
2183         else if (func->flag & FUNC_USE_SELF_TYPE) {
2184                 fprintf(f, "\tstruct StructRNA *_type;\n");
2185         }
2186
2187         dparm = dfunc->cont.properties.first;
2188         for (; dparm; dparm = dparm->next) {
2189                 type = dparm->prop->type;
2190                 flag = dparm->prop->flag;
2191                 pout = (flag & PROP_OUTPUT);
2192                 cptr = ((type == PROP_POINTER) && !(flag & PROP_RNAPTR));
2193
2194                 if (dparm->prop == func->c_ret)
2195                         ptrstr = cptr || dparm->prop->arraydimension ? "*" : "";
2196                 /* XXX only arrays and strings are allowed to be dynamic, is this checked anywhere? */
2197                 else if (cptr || (flag & PROP_DYNAMIC))
2198                         ptrstr = pout ? "**" : "*";
2199                 /* fixed size arrays and RNA pointers are pre-allocated on the ParameterList stack, pass a pointer to it */
2200                 else if (type == PROP_POINTER || dparm->prop->arraydimension)
2201                         ptrstr = "*";
2202                 else if ((type == PROP_POINTER) && (flag & PROP_RNAPTR) && !(flag & PROP_THICK_WRAP))
2203                         ptrstr = "*";
2204                 /* PROP_THICK_WRAP strings are pre-allocated on the ParameterList stack,
2205                  * but type name for string props is already (char *), so leave empty */
2206                 else if (type == PROP_STRING && (flag & PROP_THICK_WRAP))
2207                         ptrstr = "";
2208                 else
2209                         ptrstr = pout ? "*" : "";
2210
2211                 /* for dynamic parameters we pass an additional int for the length of the parameter */
2212                 if (flag & PROP_DYNAMIC)
2213                         fprintf(f, "\tint %s%s_len;\n", pout ? "*" : "", dparm->prop->identifier);
2214                 
2215                 fprintf(f, "\t%s%s %s%s;\n", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop),
2216                         ptrstr, dparm->prop->identifier);
2217         }
2218
2219         if (has_data) {
2220                 fprintf(f, "\tchar *_data");
2221                 if (func->c_ret) fprintf(f, ", *_retdata");
2222                 fprintf(f, ";\n");
2223                 fprintf(f, "\t\n");
2224         }
2225
2226         /* assign self */
2227         if (func->flag & FUNC_USE_SELF_ID) {
2228                 fprintf(f, "\t_selfid = (struct ID *)_ptr->id.data;\n");
2229         }
2230         
2231         if ((func->flag & FUNC_NO_SELF) == 0) {
2232                 if (dsrna->dnaname) fprintf(f, "\t_self = (struct %s *)_ptr->data;\n", dsrna->dnaname);
2233                 else fprintf(f, "\t_self = (struct %s *)_ptr->data;\n", srna->identifier);
2234         }
2235         else if (func->flag & FUNC_USE_SELF_TYPE) {
2236                 fprintf(f, "\t_type = _ptr->type;\n");
2237         }
2238
2239         if (has_data) {
2240                 fprintf(f, "\t_data = (char *)_parms->data;\n");
2241         }
2242
2243         dparm = dfunc->cont.properties.first;
2244         for (; dparm; dparm = dparm->next) {
2245                 type = dparm->prop->type;
2246                 flag = dparm->prop->flag;
2247                 pout = (flag & PROP_OUTPUT);
2248                 cptr = ((type == PROP_POINTER) && !(flag & PROP_RNAPTR));
2249
2250                 if (dparm->prop == func->c_ret)
2251                         fprintf(f, "\t_retdata = _data;\n");
2252                 else {
2253                         const char *data_str;
2254                         if (cptr || (flag & PROP_DYNAMIC)) {
2255                                 ptrstr = "**";
2256                                 valstr = "*";
2257                         }
2258                         else if ((type == PROP_POINTER) && !(flag & PROP_THICK_WRAP)) {
2259                                 ptrstr = "**";
2260                                 valstr = "*";
2261                         }
2262                         else if (type == PROP_POINTER || dparm->prop->arraydimension) {
2263                                 ptrstr = "*";
2264                                 valstr = "";
2265                         }
2266                         else if (type == PROP_STRING && (flag & PROP_THICK_WRAP)) {
2267                                 ptrstr = "";
2268                                 valstr = "";
2269                         }
2270                         else {
2271                                 ptrstr = "*";
2272                                 valstr = "*";
2273                         }
2274
2275                         /* this must be kept in sync with RNA_parameter_length_get_data,
2276                          * we could just call the function directly, but this is faster */
2277                         if (flag & PROP_DYNAMIC) {
2278                                 fprintf(f, "\t%s_len = %s((int *)_data);\n", dparm->prop->identifier, pout ? "" : "*");
2279                                 data_str = "(&(((char *)_data)[sizeof(void *)]))";
2280                         }
2281                         else {
2282                                 data_str = "_data";
2283                         }
2284                         fprintf(f, "\t%s = ", dparm->prop->identifier);
2285
2286                         if (!pout)
2287                                 fprintf(f, "%s", valstr);
2288
2289                         fprintf(f, "((%s%s %s)%s);\n", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop),
2290                                 ptrstr, data_str);
2291                 }
2292
2293                 if (dparm->next)
2294                         fprintf(f, "\t_data += %d;\n", rna_parameter_size_alloc(dparm->prop));
2295         }
2296
2297         if (dfunc->call) {
2298                 fprintf(f, "\t\n");
2299                 fprintf(f, "\t");
2300                 if (func->c_ret) fprintf(f, "%s = ", func->c_ret->identifier);
2301                 fprintf(f, "%s(", dfunc->call);
2302
2303                 first = 1;
2304
2305                 if (func->flag & FUNC_USE_SELF_ID) {
2306                         fprintf(f, "_selfid");
2307                         first = 0;
2308                 }
2309
2310                 if ((func->flag & FUNC_NO_SELF) == 0) {
2311                         if (!first) fprintf(f, ", ");
2312                         fprintf(f, "_self");
2313                         first = 0;
2314                 }
2315                 else if (func->flag & FUNC_USE_SELF_TYPE) {
2316                         if (!first) fprintf(f, ", ");
2317                         fprintf(f, "_type");
2318                         first = 0;
2319                 }
2320
2321                 if (func->flag & FUNC_USE_MAIN) {
2322                         if (!first) fprintf(f, ", ");
2323                         first = 0;
2324                         fprintf(f, "CTX_data_main(C)"); /* may have direct access later */
2325                 }
2326
2327                 if (func->flag & FUNC_USE_CONTEXT) {
2328                         if (!first) fprintf(f, ", ");
2329                         first = 0;
2330                         fprintf(f, "C");
2331                 }
2332
2333                 if (func->flag & FUNC_USE_REPORTS) {
2334                         if (!first) fprintf(f, ", ");
2335                         first = 0;
2336                         fprintf(f, "reports");
2337                 }
2338
2339                 dparm = dfunc->cont.properties.first;
2340                 for (; dparm; dparm = dparm->next) {
2341                         if (dparm->prop == func->c_ret)
2342                                 continue;
2343
2344                         if (!first) fprintf(f, ", ");
2345                         first = 0;
2346
2347                         if (dparm->prop->flag & PROP_DYNAMIC)
2348                                 fprintf(f, "%s_len, %s", dparm->prop->identifier, dparm->prop->identifier);
2349                         else
2350                                 fprintf(f, "%s", dparm->prop->identifier);
2351                 }
2352
2353                 fprintf(f, ");\n");
2354
2355                 if (func->c_ret) {
2356                         dparm = rna_find_parameter_def(func->c_ret);
2357                         ptrstr = (((dparm->prop->type == PROP_POINTER) && !(dparm->prop->flag & PROP_RNAPTR)) ||
2358                                   (dparm->prop->arraydimension)) ? "*" : "";
2359                         fprintf(f, "\t*((%s%s %s*)_retdata) = %s;\n", rna_type_struct(dparm->prop),
2360                                 rna_parameter_type_name(dparm->prop), ptrstr, func->c_ret->identifier);
2361                 }
2362         }
2363
2364         fprintf(f, "}\n\n");
2365
2366         dfunc->gencall = funcname;
2367 }
2368
2369 static void rna_auto_types(void)
2370 {
2371         StructDefRNA *ds;
2372         PropertyDefRNA *dp;
2373
2374         for (ds = DefRNA.structs.first; ds; ds = ds->cont.next) {
2375                 /* DNA name for Screen is patched in 2.5, we do the reverse here .. */
2376                 if (ds->dnaname && strcmp(ds->dnaname, "Screen") == 0)
2377                         ds->dnaname = "bScreen";
2378
2379                 for (dp = ds->cont.properties.first; dp; dp = dp->next) {
2380                         if (dp->dnastructname && strcmp(dp->dnastructname, "Screen") == 0)
2381                                 dp->dnastructname = "bScreen";
2382
2383                         if (dp->dnatype) {
2384                                 if (dp->prop->type == PROP_POINTER) {
2385                                         PointerPropertyRNA *pprop = (PointerPropertyRNA *)dp->prop;
2386                                         StructRNA *type;
2387
2388                                         if (!pprop->type && !pprop->get)
2389                                                 pprop->type = (StructRNA *)rna_find_type(dp->dnatype);
2390
2391                                         if (pprop->type) {
2392                                                 type = rna_find_struct((const char *)pprop->type);
2393                                                 if (type && (type->flag & STRUCT_ID_REFCOUNT))
2394                                                         pprop->property.flag |= PROP_ID_REFCOUNT;
2395                                         }
2396                                 }
2397                                 else if (dp->prop->type == PROP_COLLECTION) {
2398                                         CollectionPropertyRNA *cprop = (CollectionPropertyRNA *)dp->prop;
2399
2400                                         if (!cprop->item_type && !cprop->get && strcmp(dp->dnatype, "ListBase") == 0)
2401                                                 cprop->item_type = (StructRNA *)rna_find_type(dp->dnatype);
2402                                 }
2403                         }
2404                 }
2405         }
2406 }
2407
2408 static void rna_sort(BlenderRNA *brna)
2409 {
2410         StructDefRNA *ds;
2411         StructRNA *srna;
2412
2413         rna_sortlist(&brna->structs, cmp_struct);
2414         rna_sortlist(&DefRNA.structs, cmp_def_struct);
2415
2416         for (srna = brna->structs.first; srna; srna = srna->cont.next)
2417                 rna_sortlist(&srna->cont.properties, cmp_property);
2418
2419         for (ds = DefRNA.structs.first; ds; ds = ds->cont.next)
2420                 rna_sortlist(&ds->cont.properties, cmp_def_property);
2421 }
2422
2423 static const char *rna_property_structname(PropertyType type)
2424 {
2425         switch (type) {
2426                 case PROP_BOOLEAN: return "BoolPropertyRNA";
2427                 case PROP_INT: return "IntPropertyRNA";
2428                 case PROP_FLOAT: return "FloatPropertyRNA";
2429                 case PROP_STRING: return "StringPropertyRNA";
2430                 case PROP_ENUM: return "EnumPropertyRNA";
2431                 case PROP_POINTER: return "PointerPropertyRNA";
2432                 case PROP_COLLECTION: return "CollectionPropertyRNA";
2433                 default: return "UnknownPropertyRNA";
2434         }
2435 }
2436
2437 static const char *rna_property_subtypename(PropertySubType type)
2438 {
2439         switch (type) {
2440                 case PROP_NONE: return "PROP_NONE";
2441                 case PROP_FILEPATH: return "PROP_FILEPATH";
2442                 case PROP_FILENAME: return "PROP_FILENAME";
2443                 case PROP_DIRPATH: return "PROP_DIRPATH";
2444                 case PROP_BYTESTRING: return "PROP_BYTESTRING";
2445                 case PROP_UNSIGNED: return "PROP_UNSIGNED";
2446                 case PROP_PERCENTAGE: return "PROP_PERCENTAGE";
2447                 case PROP_FACTOR: return "PROP_FACTOR";
2448                 case PROP_ANGLE: return "PROP_ANGLE";
2449                 case PROP_TIME: return "PROP_TIME";
2450                 case PROP_DISTANCE: return "PROP_DISTANCE";
2451                 case PROP_DISTANCE_CAMERA: return "PROP_DISTANCE_CAMERA";
2452                 case PROP_COLOR: return "PROP_COLOR";
2453                 case PROP_TRANSLATION: return "PROP_TRANSLATION";
2454                 case PROP_DIRECTION: return "PROP_DIRECTION";
2455                 case PROP_MATRIX: return "PROP_MATRIX";
2456                 case PROP_EULER: return "PROP_EULER";
2457                 case PROP_QUATERNION: return "PROP_QUATERNION";
2458                 case PROP_AXISANGLE: return "PROP_AXISANGLE";
2459                 case PROP_VELOCITY: return "PROP_VELOCITY";
2460                 case PROP_ACCELERATION: return "PROP_ACCELERATION";
2461                 case PROP_XYZ: return "PROP_XYZ";
2462                 case PROP_COLOR_GAMMA: return "PROP_COLOR_GAMMA";
2463                 case PROP_COORDS: return "PROP_COORDS";
2464                 case PROP_LAYER: return "PROP_LAYER";
2465                 case PROP_LAYER_MEMBER: return "PROP_LAYER_MEMBER";
2466                 case PROP_PASSWORD: return "PROP_PASSWORD";
2467                 default:
2468                 {
2469                         /* in case we don't have a type preset that includes the subtype */
2470                         if (RNA_SUBTYPE_UNIT(type)) {
2471                                 return rna_property_subtypename(type & ~RNA_SUBTYPE_UNIT(type));
2472                         }
2473                         else {
2474                                 return "PROP_SUBTYPE_UNKNOWN";
2475                         }
2476                 }
2477         }
2478 }
2479
2480 static const char *rna_property_subtype_unit(PropertySubType type)
2481 {
2482         switch (RNA_SUBTYPE_UNIT(type)) {
2483                 case PROP_UNIT_NONE:         return "PROP_UNIT_NONE";
2484                 case PROP_UNIT_LENGTH:       return "PROP_UNIT_LENGTH";
2485                 case PROP_UNIT_AREA:         return "PROP_UNIT_AREA";
2486                 case PROP_UNIT_VOLUME:       return "PROP_UNIT_VOLUME";
2487                 case PROP_UNIT_MASS:         return "PROP_UNIT_MASS";
2488                 case PROP_UNIT_ROTATION:     return "PROP_UNIT_ROTATION";
2489                 case PROP_UNIT_TIME:         return "PROP_UNIT_TIME";
2490                 case PROP_UNIT_VELOCITY:     return "PROP_UNIT_VELOCITY";
2491                 case PROP_UNIT_ACCELERATION: return "PROP_UNIT_ACCELERATION";
2492                 case PROP_UNIT_CAMERA:       return "PROP_UNIT_CAMERA";
2493                 default:                     return "PROP_UNIT_UNKNOWN";
2494         }
2495 }
2496
2497 static void rna_generate_prototypes(BlenderRNA *brna, FILE *f)
2498 {
2499         StructRNA *srna;
2500
2501         for (srna = brna->structs.first; srna; srna = srna->cont.next)
2502                 fprintf(f, "extern StructRNA RNA_%s;\n", srna->identifier);
2503         fprintf(f, "\n");
2504 }
2505
2506 static void rna_generate_blender(BlenderRNA *brna, FILE *f)
2507 {
2508         StructRNA *srna;
2509
2510         fprintf(f, "BlenderRNA BLENDER_RNA = {");
2511
2512         srna = brna->structs.first;
2513         if (srna) fprintf(f, "{&RNA_%s, ", srna->identifier);
2514         else fprintf(f, "{NULL, ");
2515
2516         srna = brna->structs.last;
2517         if (srna) fprintf(f, "&RNA_%s}", srna->identifier);
2518         else fprintf(f, "NULL}");
2519
2520         fprintf(f, "};\n\n");
2521 }
2522
2523 static void rna_generate_property_prototypes(BlenderRNA *UNUSED(brna), StructRNA *srna, FILE *f)
2524 {
2525         PropertyRNA *prop;
2526         StructRNA *base;
2527
2528         base = srna->base;
2529         while (base) {
2530                 fprintf(f, "\n");
2531                 for (prop = base->cont.properties.first; prop; prop = prop->next)
2532                         fprintf(f, "%s%s rna_%s_%s;\n", "extern ", rna_property_structname(prop->type),
2533                                 base->identifier, prop->identifier);
2534                 base = base->base;
2535         }
2536
2537         if (srna->cont.properties.first)
2538                 fprintf(f, "\n");
2539
2540         for (prop = srna->cont.properties.first; prop; prop = prop->next)
2541                 fprintf(f, "%s%s rna_%s_%s;\n", (prop->flag & PROP_EXPORT) ? "" : "", rna_property_structname(prop->type),
2542                         srna->identifier, prop->identifier);
2543         fprintf(f, "\n");
2544 }
2545
2546 static void rna_generate_parameter_prototypes(BlenderRNA *UNUSED(brna), StructRNA *srna, FunctionRNA *func, FILE *f)
2547 {
2548         PropertyRNA *parm;
2549
2550         for (parm = func->cont.properties.first; parm; parm = parm->next)
2551                 fprintf(f, "%s%s rna_%s_%s_%s;\n", "extern ", rna_property_structname(parm->type), srna->identifier,
2552                         func->identifier, parm->identifier);
2553
2554         if (func->cont.properties.first)
2555                 fprintf(f, "\n");
2556 }
2557
2558 static void rna_generate_function_prototypes(BlenderRNA *brna, StructRNA *srna, FILE *f)
2559 {
2560         FunctionRNA *func;
2561         StructRNA *base;
2562
2563         base = srna->base;
2564         while (base) {
2565                 for (func = base->functions.first; func; func = func->cont.next) {
2566                         fprintf(f, "%s%s rna_%s_%s_func;\n", "extern ", "FunctionRNA", base->identifier, func->identifier);
2567                         rna_generate_parameter_prototypes(brna, base, func, f);
2568                 }
2569
2570                 if (base->functions.first)
2571                         fprintf(f, "\n");
2572
2573                 base = base->base;
2574         }
2575
2576         for (func = srna->functions.first; func; func = func->cont.next) {
2577                 fprintf(f, "%s%s rna_%s_%s_func;\n", "extern ", "FunctionRNA", srna->identifier, func->identifier);
2578                 rna_generate_parameter_prototypes(brna, srna, func, f);
2579         }
2580
2581         if (srna->functions.first)
2582                 fprintf(f, "\n");
2583 }
2584
2585 static void rna_generate_static_parameter_prototypes(FILE *f, StructRNA *srna, FunctionDefRNA *dfunc, const char *name_override, int close_prototype)
2586 {
2587         FunctionRNA *func;
2588         PropertyDefRNA *dparm;
2589         StructDefRNA *dsrna;
2590         PropertyType type;
2591         int flag, pout, cptr, first;
2592         const char *ptrstr;
2593
2594         dsrna = rna_find_struct_def(srna);
2595         func = dfunc->func;
2596
2597         /* return type */
2598         for (dparm = dfunc->cont.properties.first; dparm; dparm = dparm->next) {
2599                 if (dparm->prop == func->c_ret) {
2600                         if (dparm->prop->arraydimension)
2601                                 fprintf(f, "XXX no array return types yet");  /* XXX not supported */
2602                         else if (dparm->prop->type == PROP_POINTER && !(dparm->prop->flag & PROP_RNAPTR))
2603                                 fprintf(f, "%s%s *", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop));
2604                         else
2605                                 fprintf(f, "%s%s ", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop));
2606
2607                         break;
2608                 }
2609         }
2610
2611         /* void if nothing to return */
2612         if (!dparm)
2613                 fprintf(f, "void ");
2614
2615         /* function name */
2616         if (name_override == NULL || name_override[0] == '\0')
2617                 fprintf(f, "%s(", dfunc->call);
2618         else
2619                 fprintf(f, "%s(", name_override);
2620
2621         first = 1;
2622
2623         /* self, context and reports parameters */
2624         if (func->flag & FUNC_USE_SELF_ID) {
2625                 fprintf(f, "struct ID *_selfid");
2626                 first = 0;
2627         }
2628
2629         if ((func->flag & FUNC_NO_SELF) == 0) {
2630                 if (!first) fprintf(f, ", ");
2631                 if (dsrna->dnaname) fprintf(f, "struct %s *_self", dsrna->dnaname);
2632                 else fprintf(f, "struct %s *_self", srna->identifier);
2633                 first = 0;
2634         }
2635         else if (func->flag & FUNC_USE_SELF_TYPE) {
2636                 if (!first) fprintf(f, ", ");
2637                 fprintf(f, "struct StructRNA *_type");
2638                 first = 0;
2639         }
2640
2641         if (func->flag & FUNC_USE_MAIN) {
2642                 if (!first) fprintf(f, ", ");
2643                 first = 0;
2644                 fprintf(f, "Main *bmain");
2645         }
2646
2647         if (func->flag & FUNC_USE_CONTEXT) {
2648                 if (!first) fprintf(f, ", ");
2649                 first = 0;
2650                 fprintf(f, "bContext *C");
2651         }
2652
2653         if (func->flag & FUNC_USE_REPORTS) {
2654                 if (!first) fprintf(f, ", ");
2655                 first = 0;
2656                 fprintf(f, "ReportList *reports");
2657         }
2658
2659         /* defined parameters */
2660         for (dparm = dfunc->cont.properties.first; dparm; dparm = dparm->next) {
2661                 type = dparm->prop->type;
2662                 flag = dparm->prop->flag;
2663                 pout = (flag & PROP_OUTPUT);
2664                 cptr = ((type == PROP_POINTER) && !(flag & PROP_RNAPTR));
2665
2666                 if (dparm->prop == func->c_ret)
2667                         continue;
2668
2669                 if (cptr || (flag & PROP_DYNAMIC))
2670                         ptrstr = pout ? "**" : "*";
2671                 else if (type == PROP_POINTER || dparm->prop->arraydimension)
2672                         ptrstr = "*";
2673                 else if (type == PROP_STRING && (flag & PROP_THICK_WRAP))
2674                         ptrstr = "";
2675                 else
2676                         ptrstr = pout ? "*" : "";
2677
2678                 if (!first) fprintf(f, ", ");
2679                 first = 0;
2680
2681                 if (flag & PROP_DYNAMIC)
2682                         fprintf(f, "int %s%s_len, ", pout ? "*" : "", dparm->prop->identifier);
2683
2684                 if (!(flag & PROP_DYNAMIC) && dparm->prop->arraydimension)
2685                         fprintf(f, "%s%s %s[%u]", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop),
2686                                 rna_safe_id(dparm->prop->identifier), dparm->prop->totarraylength);
2687                 else
2688                         fprintf(f, "%s%s %s%s", rna_type_struct(dparm->prop), rna_parameter_type_name(dparm->prop),
2689                                 ptrstr, rna_safe_id(dparm->prop->identifier));
2690
2691         }
2692
2693         fprintf(f, ")");
2694         if (close_prototype)
2695                 fprintf(f, ";\n");
2696 }
2697
2698 static void rna_generate_static_function_prototypes(BlenderRNA *UNUSED(brna), StructRNA *srna, FILE *f)
2699 {
2700         FunctionRNA *func;
2701         FunctionDefRNA *dfunc;
2702         int first = 1;
2703
2704         for (func = srna->functions.first; func; func = func->cont.next) {
2705                 dfunc = rna_find_function_def(func);
2706
2707                 if (dfunc->call) {
2708                         if (first) {
2709                                 fprintf(f, "/* Repeated prototypes to detect errors */\n\n");
2710                                 first = 0;
2711                         }
2712
2713                         rna_generate_static_parameter_prototypes(f, srna, dfunc, NULL, 1);
2714                 }
2715         }
2716
2717         fprintf(f, "\n");
2718 }
2719
2720 static void rna_generate_struct_prototypes(FILE *f)
2721 {
2722         StructDefRNA *ds;
2723         PropertyDefRNA *dp;
2724         FunctionDefRNA *dfunc;
2725         const char *structures[2048];
2726         int all_structures = 0;
2727
2728         /* structures definitions */
2729         for (ds = DefRNA.structs.first; ds; ds = ds->cont.next) {
2730                 for (dfunc = ds->functions.first; dfunc; dfunc = dfunc->cont.next) {
2731                         if (dfunc->call) {
2732                                 for (dp = dfunc->cont.properties.first; dp; dp = dp->next) {
2733                                         if (dp->prop->type == PROP_POINTER) {
2734                                                 int a, found = 0;
2735                                                 const char *struct_name = rna_parameter_type_name(dp->prop);
2736
2737                                                 for (a = 0; a < all_structures; a++) {
2738                                                         if (strcmp(struct_name, structures[a]) == 0) {
2739                                                                 found = 1;
2740                                                                 break;
2741                                                         }
2742                                                 }
2743
2744                                                 if (found == 0) {
2745                                                         fprintf(f, "struct %s;\n", struct_name);
2746
2747                                                         if (all_structures >= sizeof(structures) / sizeof(structures[0])) {
2748                                                                 printf("Array size to store all structures names is too small\n");
2749                                                                 exit(1);
2750                                                         }
2751
2752                                                         structures[all_structures++] = struct_name;
2753                                                 }
2754                                         }
2755                                 }
2756                         }
2757                 }
2758         }
2759
2760         fprintf(f, "\n");
2761 }
2762
2763 static void rna_generate_property(FILE *f, StructRNA *srna, const char *nest, PropertyRNA *prop)
2764 {
2765         char *strnest = "", *errnest = "";
2766         int len, freenest = 0;
2767         
2768         if (nest != NULL) {
2769                 len = strlen(nest);
2770
2771                 strnest = MEM_mallocN(sizeof(char) * (len + 2), "rna_generate_property -> strnest");
2772                 errnest = MEM_mallocN(sizeof(char) * (len + 2), "rna_generate_property -> errnest");
2773
2774                 strcpy(strnest, "_"); strcat(strnest, nest);
2775                 strcpy(errnest, "."); strcat(errnest, nest);
2776
2777                 freenest = 1;
2778         }
2779
2780         switch (prop->type) {
2781                 case PROP_ENUM:
2782                 {
2783                         EnumPropertyRNA *eprop = (EnumPropertyRNA *)prop;
2784                         int i, defaultfound = 0, totflag = 0;
2785
2786                         if (eprop->item) {
2787                                 fprintf(f, "static EnumPropertyItem rna_%s%s_%s_items[%d] = {\n\t", srna->identifier,
2788                                         strnest, prop->identifier, eprop->totitem + 1);
2789
2790                                 for (i = 0; i < eprop->totitem; i++) {
2791                                         fprintf(f, "{%d, ", eprop->item[i].value);
2792                                         rna_print_c_string(f, eprop->item[i].identifier); fprintf(f, ", ");
2793                                         fprintf(f, "%d, ", eprop->item[i].icon);
2794                                         rna_print_c_string(f, eprop->item[i].name); fprintf(f, ", ");
2795                                         rna_print_c_string(f, eprop->item[i].description); fprintf(f, "},\n\t");
2796
2797                                         if (eprop->item[i].identifier[0]) {
2798                                                 if (prop->flag & PROP_ENUM_FLAG) {
2799                                                         totflag |= eprop->item[i].value;
2800                                                 }
2801                                                 else {
2802                                                         if (eprop->defaultvalue == eprop->item[i].value) {
2803                                                                 defaultfound = 1;
2804                                                         }
2805                                                 }
2806                                         }
2807                                 }
2808
2809                                 fprintf(f, "{0, NULL, 0, NULL, NULL}\n};\n\n");
2810
2811                                 if (prop->flag & PROP_ENUM_FLAG) {
2812                                         if (eprop->defaultvalue & ~totflag) {
2813                                                 fprintf(stderr, "%s: %s%s.%s, enum default includes unused bits (%d).\n",
2814                                                         __func__, srna->identifier, errnest, prop->identifier,
2815                                                         eprop->defaultvalue & ~totflag);
2816                                                 DefRNA.error = 1;
2817                                         }
2818                                 }
2819                                 else {
2820                                         if (!defaultfound) {
2821                                                 fprintf(stderr, "%s: %s%s.%s, enum default is not in items.\n",
2822                                                         __func__, srna->identifier, errnest, prop->identifier);
2823                                                 DefRNA.error = 1;
2824                                         }
2825                                 }
2826                         }
2827                         else {
2828                                 fprintf(stderr, "%s: %s%s.%s, enum must have items defined.\n",
2829                                         __func__, srna->identifier, errnest, prop->identifier);
2830                                 DefRNA.error = 1;
2831                         }
2832                         break;
2833                 }
2834                 case PROP_BOOLEAN:
2835                 {
2836                         BoolPropertyRNA *bprop = (BoolPropertyRNA *)prop;
2837                         unsigned int i;
2838
2839                         if (prop->arraydimension && prop->totarraylength) {
2840                                 fprintf(f, "static int rna_%s%s_%s_default[%u] = {\n\t", srna->identifier, strnest,
2841                                         prop->identifier, prop->totarraylength);
2842
2843                                 for (i = 0; i < prop->totarraylength; i++) {
2844                                         if (bprop->defaultarray)
2845                                                 fprintf(f, "%d", bprop->defaultarray[i]);
2846                                         else
2847                                                 fprintf(f, "%d", bprop->defaultvalue);
2848                                         if (i != prop->totarraylength - 1)
2849                                                 fprintf(f, ",\n\t");
2850                                 }
2851
2852                                 fprintf(f, "\n};\n\n");
2853                         }
2854                         break;
2855                 }
2856                 case PROP_INT:
2857       &