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