28#define DNA_DEPRECATED_ALLOW
71#define DEBUG_PRINTF(debug_level, ...) \
73 if (debugSDNA > debug_level) { \
74 printf(__VA_ARGS__); \
125 this->types.
add_new({.name = name,
131 .is_struct =
false});
137 this->types.
add({.name = name,
223 const char *cp = parsed_member.
member_name.c_str();
224 const int namelen = int(parsed_member.
member_name.size());
227 if (cp[0] ==
'*' || cp[1] ==
'*') {
228 return sizeof(
void *) * array_num;
236 const int member_align_override,
241 const char *struct_name = struct_info.
name.c_str();
243 if (member_align_override > 0 && (
len % member_align_override)) {
245 "Align %d error (%s) in struct: %s %s (add %d padding bytes)\n",
246 member_align_override,
250 member_align_override - (
len % member_align_override));
253 if (!member_type.
is_struct && type_size > 4 && (
len % 8)) {
255 "Align 8 error (%s) in struct: %s %s (add %d padding bytes)\n",
262 if (type_size > 3 && (
len % 4)) {
264 "Align 4 error (%s) in struct: %s %s (add %d padding bytes)\n",
271 if (type_size == 2 && (
len % 2)) {
273 "Align 2 error (%s) in struct: %s %s (add %d padding bytes)\n",
287 bool dna_error =
false;
290 int unknown = int(parsed_structs.
size());
292 const int lastunknown = unknown;
304 int max_align_32 = 0;
305 int max_align_64 = 0;
310 const char *cp = parsed_member.
member_name.c_str();
311 const int namelen = int(parsed_member.
member_name.size());
314 if (cp[0] ==
'*' || cp[1] ==
'*') {
318 if (array_num == 0) {
320 "Zero array size found or could not parse %s: '%.*s'\n",
321 struct_info.
name.c_str(),
328 if (
sizeof(
void *) == 4) {
329 if (size_native % 4) {
331 "Align pointer error in struct (size_native 4): %s %s\n",
332 struct_info.
name.c_str(),
338 if (size_native % 8) {
340 "Align pointer error in struct (size_native 8): %s %s\n",
341 struct_info.
name.c_str(),
349 "Align pointer error in struct (size_64 8): %s %s\n",
350 struct_info.
name.c_str(),
356 size_32 += 4 * array_num;
357 size_64 += 8 * array_num;
358 max_align_32 = std::max(max_align_32, 4);
359 max_align_64 = std::max(max_align_64, 8);
361 else if (cp[0] ==
'[') {
365 "Parse error in struct, invalid member name: %s %s\n",
366 struct_info.
name.c_str(),
374 if (array_num == 0) {
376 "Zero array size found or could not parse %s: '%.*s'\n",
377 struct_info.
name.c_str(),
385 if (
sizeof(
void *) == 8 && (size_native % 8)) {
387 "Align struct error: %s::%s (starts at %d on the native platform; "
388 "%d %% %zu = %d bytes)\n",
389 struct_info.
name.c_str(),
400 const int member_align = parsed_member.
alignment;
404 struct_info, member_type, size_32, member_align, cp,
"32 bit"))
409 struct_info, member_type, size_64, member_align, cp,
"64 bit"))
415 size_32 += array_num * member_type.
size_32;
416 size_64 += array_num * member_type.
size_64;
417 max_align_32 = std::max<int>(max_align_32, member_type.
align_32);
418 max_align_64 = std::max<int>(max_align_64, member_type.
align_64);
419 max_align_32 = std::max<int>(max_align_32, member_align);
420 max_align_64 = std::max<int>(max_align_64, member_align);
430 if (size_native == 0) {
440 struct_info.
size_32 = short(size_32);
441 struct_info.
size_64 = short(size_64);
442 struct_info.
align_32 = short(max_align_32);
443 struct_info.
align_64 = short(max_align_64);
455 if (size_32 % max_align_32) {
459 if ((size_64 % max_align_64 == 0) && (size_32 % max_align_32 == 4)) {
461 "Sizeerror in 32 bit struct: %s (add padding pointer)\n",
462 struct_info.
name.c_str());
466 "Sizeerror in 32 bit struct: %s (add %d bytes)\n",
467 struct_info.
name.c_str(),
468 max_align_32 - (size_32 % max_align_32));
473 if (size_64 % max_align_64) {
475 "Sizeerror in 64 bit struct: %s (add %d bytes)\n",
476 struct_info.
name.c_str(),
477 max_align_64 - (size_64 % max_align_64));
481 if (size_native % 4 && !
ELEM(size_native, 1, 2)) {
483 "Sizeerror 4 in struct: %s (add %d bytes)\n",
484 struct_info.
name.c_str(),
492 if (unknown == lastunknown) {
498 fprintf(stderr,
"ERROR: still %d structs unknown\n", unknown);
501 fprintf(stderr,
"*** Known structs :\n");
505 fprintf(stderr,
" %s\n", info.
name.c_str());
510 fprintf(stderr,
"*** Unknown structs :\n");
514 fprintf(stderr,
" %s\n", info.
name.c_str());
531static void dna_write(FILE *file,
const void *pntr,
const int size)
533 constexpr int MAX_DNA_LINE_LENGTH = 20;
534 static int linelength = 0;
535 const char *
data =
static_cast<const char *
>(pntr);
537 for (
int i = 0; i < size; i++) {
538 fprintf(file,
"%d, ",
data[i]);
540 if (linelength >= MAX_DNA_LINE_LENGTH) {
553 const int num_structs = 1 + int(parsed_structs.
size());
565 printf(
"names_len %d types_len %d structs_len %d\n",
566 int(member_names.
size()),
567 int(table.
types.size()),
570 printf(
" %s\n", name.c_str());
581 printf(
" struct %s elems: %d size: %d\n",
583 int(ps.members.size()),
587 printf(
" %s %s allign32:%d, allign64:%d\n",
588 member_type.
name.c_str(),
600 const char nil_bytes[4] = {0};
606 int len = int(member_names.
size());
610 const int member_len = int(member_name.size()) + 1;
611 dna_write(file, member_name.c_str(), member_len);
614 int len_align = (
len + 3) & ~3;
615 if (
len != len_align) {
625 const int type_len = int(type.
name.size()) + 1;
629 len_align = (
len + 3) & ~3;
630 if (
len != len_align) {
641 if (table.
types.size() & 1) {
652 const short raw_data_header[2] = {short(table.
lookup_index(
"raw_data")), 0};
656 const short header[2] = {short(table.
lookup_index(ps.type_name)), short(ps.members.size())};
677 fprintf(file,
"#pragma once\n");
678 fprintf(file,
"#define SDNA_TYPE_FROM_STRUCT(id) _SDNA_TYPE_##id\n");
679 fprintf(file,
"enum {\n");
682 const int sdna_index = int(i) + 1;
684 file,
"\t_SDNA_TYPE_%s = %d,\n", parsed_structs[i].alias_type_name.c_str(), sdna_index);
686 fprintf(file,
"\tSDNA_TYPE_MAX = %d,\n", 1 +
int(parsed_structs.
size()));
687 fprintf(file,
"};\n\n");
693 fprintf(file,
"#include \"DNA_sdna_type_ids.hh\"\n\n");
694 fprintf(file,
"namespace blender {\n");
695 fprintf(file,
"namespace dna {\n\n");
696 fprintf(file,
"int sdna_struct_id_get_max() { return %d; }\n",
int(parsed_structs.
size()));
697 fprintf(file,
"\n}\n");
700 const char *name = parsed_structs[i].alias_type_name.c_str();
701 const int sdna_index = int(i) + 1;
702 fprintf(file,
"struct %s;\n", name);
704 file,
"template<> int dna::sdna_struct_id_get<%s>() { return %d; }\n", name, sdna_index);
707 fprintf(file,
"\n}\n");
715 fprintf(file,
"/* Default struct member values for RNA. */\n");
716 fprintf(file,
"#define DNA_DEPRECATED_ALLOW\n");
717 fprintf(file,
"#define DNA_NO_EXTERNAL_CONSTRUCTORS\n");
718 fprintf(file,
"#include \"DNA_sdna_type_ids.hh\"\n\n");
721 fprintf(file,
"#include \"%s%s\"\n", base_directory.
c_str(),
filename);
724 fprintf(file,
"namespace blender {\n\n");
729 std::string default_var;
731 if (name ==
"bTheme") {
733 fprintf(file,
"extern \"C\" const bTheme U_theme_default;\n");
734 default_var =
"U_theme_default";
737 fprintf(file,
"static const %s DNA_DEFAULT_%s = {};\n", name.
c_str(), name.
c_str());
738 default_var =
"DNA_DEFAULT_" + name;
742 fprintf(file,
"static const void *const member_defaults_%s[] = {\n", name.
c_str());
745 fprintf(file,
" &%s.%.*s,\n", default_var.c_str(),
int(bare_id.
size()), bare_id.
data());
747 fprintf(file,
"};\n");
751 fprintf(file,
"\nextern const void *const *const DNA_member_default_table[] = {\n");
753 fprintf(file,
" member_defaults_%s,\n", parsed_struct.type_name.c_str());
755 fprintf(file,
"};\n\n");
757 fprintf(file,
"} // namespace blender\n");
766 fprintf(file,
"/* Verify struct sizes and member offsets are as expected by DNA. */\n");
767 fprintf(file,
"#include \"BLI_assert.hh\"\n\n");
769 fprintf(file,
"#define DNA_DEPRECATED_ALLOW\n");
772 fprintf(file,
"#define assert_line_ assert_line_DNA_\n");
774 fprintf(file,
"#include \"%s%s\"\n", base_directory.
c_str(),
filename);
776 fprintf(file,
"#undef assert_line_\n");
778 fprintf(file,
"using namespace blender;\n");
787 "BLI_STATIC_ASSERT(offsetof(struct %s, %.*s) == %d, \"DNA member offset "
789 parsed_struct.alias_type_name.c_str(),
790 int(alias_id.
size()),
795 const TypeInfo &struct_info = table.
lookup(parsed_struct.type_name);
797 "BLI_STATIC_ASSERT(sizeof(struct %s) == %d, \"DNA struct size verify\");\n\n",
798 parsed_struct.alias_type_name.c_str(),
817 parsed_struct.alias_type_name = parsed_struct.type_name;
818 parsed_struct.type_name = rename_maps.
types.lookup_default_as(parsed_struct.type_name,
819 parsed_struct.type_name);
822 members_unique.
reserve(parsed_struct.members.size());
831 std::string &member_name = parsed_member.
member_name;
834 {parsed_struct.type_name, std::string(stripped)});
835 if (member_static !=
nullptr) {
836 const size_t prefix_len = stripped.
data() - member_name.data();
837 member_name.replace(prefix_len, stripped.
size(), *member_static);
842 if (!members_unique.
add(stripped)) {
844 "Error: duplicate name found '%s.%.*s', "
845 "likely cause is 'dna_rename_defs.h'\n",
846 parsed_struct.alias_type_name.c_str(),
847 int(stripped.
size()),
930 FILE *fp = fopen(
filename.c_str(),
"w");
932 "#error \"Error! can't make correct DNA.c file from %s:%d, check alignment.\"\n",
939# define BASE_HEADER "../"
945 "Usage: %s [--include-file <file>, ...] "
946 "dna.cc dna_type_offsets.h dna_verify.cc dna_struct_ids.cc dna_defaults.cc "
947 "[base directory]\n",
966 while (arg_index < argc) {
967 if (
STREQ(argv[arg_index],
"--include-file")) {
969 if (arg_index == argc) {
970 printf(
"Missing argument for --include-file\n");
974 cli_include_files.
append(argv[arg_index]);
981 if (!cli_include_files.
is_empty()) {
986 const int num_arguments = argc - arg_index;
987 if (!
ELEM(num_arguments, 5, 6)) {
992 int return_status = 0;
994 FILE *file_dna = fopen(argv[arg_index],
"w");
995 FILE *file_dna_offsets = fopen(argv[arg_index + 1],
"w");
996 FILE *file_dna_verify = fopen(argv[arg_index + 2],
"w");
997 FILE *file_dna_ids = fopen(argv[arg_index + 3],
"w");
998 FILE *file_dna_defaults = fopen(argv[arg_index + 4],
"w");
1000 printf(
"Unable to open file: %s\n", argv[arg_index]);
1003 else if (!file_dna_offsets) {
1004 printf(
"Unable to open file: %s\n", argv[arg_index + 1]);
1007 else if (!file_dna_verify) {
1008 printf(
"Unable to open file: %s\n", argv[arg_index + 2]);
1011 else if (!file_dna_ids) {
1012 printf(
"Unable to open file: %s\n", argv[arg_index + 3]);
1015 else if (!file_dna_defaults) {
1016 printf(
"Unable to open file: %s\n", argv[arg_index + 4]);
1020 const char *base_directory;
1022 if (num_arguments == 6) {
1023 base_directory = argv[arg_index + 5];
1033# define FORCE_ALIGN_4 " __attribute__((aligned(4))) "
1035# define FORCE_ALIGN_4 " "
1037 fprintf(file_dna,
"extern const unsigned char DNAstr[];\n");
1038 fprintf(file_dna,
"const unsigned char" FORCE_ALIGN_4 "DNAstr[] = {\n");
1055 fprintf(file_dna,
"};\n");
1056 fprintf(file_dna,
"extern const int DNAlen;\n");
1057 fprintf(file_dna,
"const int DNAlen = sizeof(DNAstr);\n");
1064 if (file_dna_offsets) {
1065 fclose(file_dna_offsets);
1067 if (file_dna_verify) {
1068 fclose(file_dna_verify);
1071 fclose(file_dna_ids);
1073 if (file_dna_defaults) {
1074 fclose(file_dna_defaults);
1077 return return_status;
1088# pragma GCC diagnostic error "-Wpadded"
1095#include "dna_includes_all.h"
1112#define DNA_STRUCT_RENAME(old, new) (void)sizeof(new);
1113#define DNA_STRUCT_RENAME_MEMBER(new_struct_name, old, new) (void)offsetof(new_struct_name, new);
1116#undef DNA_STRUCT_RENAME
1117#undef DNA_STRUCT_RENAME_MEMBER
blenloader genfile private function prototypes
#define SDNA_RAW_DATA_STRUCT_INDEX
#define UNUSED_FUNCTION(x)
#define UNUSED_VARS_NDEBUG(...)
#define BLI_assert_msg(a, msg)
void reserve(const int64_t n)
constexpr int64_t size() const
constexpr IndexRange index_range() const
constexpr bool is_empty() const
constexpr const char * c_str() const
StringRef operator()(const TypeInfo &info) const
int64_t index_of_as(const ForwardKey &key) const
void add_new(const Key &key)
void append(const T &value)
constexpr int64_t size() const
constexpr const char * data() const
#define DEBUG_PRINTF(debug_level,...)
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
bool substitute_cpp_types(Vector< ParsedStruct > &structs, const Span< ParsedEnum > enums, bool)
bool parse_dna_headers(const StringRefNull base_directory, Vector< ParsedStruct > &r_structs, Vector< ParsedEnum > &r_enums, Span< const char * > include_files)
Span< const char * > default_dna_header_filenames()
std::string filename(const std::string_view &str, size_t pos)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
BLI_STATIC_ASSERT(ARRAY_SIZE(data_mode_strings)==CTX_MODE_NUM+1, "Must have a string for each context mode") const char *CTX_data_mode_string(const bContext *C)
static void write_sdna_type_offsets(FILE *file, const Span< dna::ParsedStruct > parsed_structs)
static TypeTable build_type_table(const Span< dna::ParsedStruct > parsed_structs)
static bool apply_renames(Vector< dna::ParsedStruct > &parsed_structs)
static int compute_type_size_and_alignment(TypeTable &table, Span< dna::ParsedStruct > parsed_structs)
BMesh const char void * data
VectorSet< T, InlineBufferCapacity, DefaultProbingStrategy, CustomIDHash< T, GetIDFn >, CustomIDEqual< T, GetIDFn > > CustomIDVectorSet
StringRef DNA_member_id_string_ref(const StringRef member_full)
static void dna_write(FILE *file, const void *pntr, const int size)
DnaRenameMaps DNA_rename_maps_alias_to_static()
static bool check_member_alignment(const TypeInfo &struct_info, const TypeInfo &member_type, const int len, const int member_align_override, const StringRefNull name, const StringRefNull detail)
void BLI_system_backtrace(FILE *fp)
static Span< const char * > includefiles
static void write_rna_defaults(FILE *file, const StringRefNull base_directory, const Span< dna::ParsedStruct > parsed_structs)
static void print_usage(const char *argv0)
int DNA_member_array_num(const StringRef str)
static void write_sdna_verify(FILE *file, const TypeTable &table, const Span< dna::ParsedStruct > parsed_structs, const StringRefNull base_directory)
static int member_size_native(const TypeInfo &member_type, const dna::ParsedMember &parsed_member)
static void make_bad_file(const StringRefNull filename, int line)
static void write_sdna_struct_ids(FILE *file, const Span< dna::ParsedStruct > parsed_structs)
static bool make_structDNA(const StringRefNull base_directory, FILE *file, FILE *file_offsets, FILE *file_verify, FILE *file_ids, FILE *file_defaults)
static void write_sdna_blob(FILE *file, const TypeTable &table, const Span< dna::ParsedStruct > parsed_structs)
Map< std::pair< StringRef, StringRef >, StringRef > members
Map< StringRef, StringRef > types
int lookup_index(StringRefNull name) const
const TypeInfo & lookup(StringRefNull name) const
void add_struct(StringRefNull name)
CustomIDVectorSet< TypeInfo, GetIDFn > types
void add_builtin(const StringRef name, const short size, const int expected_type_index)
TypeInfo & lookup(StringRefNull name)
std::string alias_member_name