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