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