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