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