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