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