<commit_before>/*
 * Copyright 2014 Google Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

// independent from idl_parser, since this code is not needed for most clients

#include "flatbuffers/flatbuffers.h"
#include "flatbuffers/idl.h"
#include "flatbuffers/util.h"
#include "flatbuffers/code_generators.h"

namespace flatbuffers {
namespace cpp {

// This tracks the current namespace so we can insert namespace declarations.
// TODO(wvo): this needs to be moved into a code generator context object.
static const Namespace *code_generator_cur_name_space = nullptr;

// Ensure that a type is prefixed with its namespace whenever it is used
// outside of its namespace.
static std::string WrapInNameSpace(const Namespace *ns,
                                   const std::string &name) {
  if (code_generator_cur_name_space != ns) {
    std::string qualified_name;
    for (auto it = ns->components.begin();
             it != ns->components.end(); ++it) {
      qualified_name += *it + "::";
    }
    return qualified_name + name;
  } else {
    return name;
  }
}

static std::string WrapInNameSpace(const Definition &def) {
  return WrapInNameSpace(def.defined_namespace, def.name);
}

// Translates a qualified name in flatbuffer text format to the same name in
// the equivalent C++ namespace.
static std::string TranslateNameSpace(const std::string &qualified_name) {
  std::string cpp_qualified_name = qualified_name;
  size_t start_pos = 0;
  while((start_pos = cpp_qualified_name.find(".", start_pos)) !=
         std::string::npos) {
    cpp_qualified_name.replace(start_pos, 1, "::");
  }
  return cpp_qualified_name;
}


// Return a C++ type from the table in idl.h
static std::string GenTypeBasic(const Type &type, bool user_facing_type) {
  static const char *ctypename[] = {
    #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE, NTYPE, PTYPE) \
      #CTYPE,
      FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
    #undef FLATBUFFERS_TD
  };
  if (user_facing_type) {
    if (type.enum_def) return WrapInNameSpace(*type.enum_def);
    if (type.base_type == BASE_TYPE_BOOL) return "bool";
  }
  return ctypename[type.base_type];
}

static std::string GenTypeWire(const Parser &parser, const Type &type,
                               const char *postfix, bool user_facing_type);

// Return a C++ pointer type, specialized to the actual struct/table types,
// and vector element types.
static std::string GenTypePointer(const Parser &parser, const Type &type) {
  switch (type.base_type) {
    case BASE_TYPE_STRING:
      return "flatbuffers::String";
    case BASE_TYPE_VECTOR:
      return "flatbuffers::Vector<" +
             GenTypeWire(parser, type.VectorType(), "", false) + ">";
    case BASE_TYPE_STRUCT: {
      return WrapInNameSpace(*type.struct_def);
    }
    case BASE_TYPE_UNION:
      // fall through
    default:
      return "void";
  }
}

// Return a C++ type for any type (scalar/pointer) specifically for
// building a flatbuffer.
static std::string GenTypeWire(const Parser &parser, const Type &type,
                               const char *postfix, bool user_facing_type) {
  return IsScalar(type.base_type)
    ? GenTypeBasic(type, user_facing_type) + postfix
    : IsStruct(type)
      ? "const " + GenTypePointer(parser, type) + " *"
      : "flatbuffers::Offset<" + GenTypePointer(parser, type) + ">" + postfix;
}

// Return a C++ type for any type (scalar/pointer) that reflects its
// serialized size.
static std::string GenTypeSize(const Parser &parser, const Type &type) {
  return IsScalar(type.base_type)
    ? GenTypeBasic(type, false)
    : IsStruct(type)
      ? GenTypePointer(parser, type)
      : "flatbuffers::uoffset_t";
}

// Return a C++ type for any type (scalar/pointer) specifically for
// using a flatbuffer.
static std::string GenTypeGet(const Parser &parser, const Type &type,
                              const char *afterbasic, const char *beforeptr,
                              const char *afterptr, bool user_facing_type) {
  return IsScalar(type.base_type)
    ? GenTypeBasic(type, user_facing_type) + afterbasic
    : beforeptr + GenTypePointer(parser, type) + afterptr;
}

static std::string GenEnumDecl(const EnumDef &enum_def,
                               const IDLOptions &opts) {
  return (opts.scoped_enums ? "enum class " : "enum ") + enum_def.name;
}

static std::string GenEnumVal(const EnumDef &enum_def,
                              const std::string &enum_val,
                              const IDLOptions &opts) {
  return opts.prefixed_enums ? enum_def.name + "_" + enum_val : enum_val;
}

static std::string GetEnumVal(const EnumDef &enum_def, const EnumVal &enum_val,
                              const IDLOptions &opts) {
  if (opts.scoped_enums) {
      return enum_def.name + "::" + enum_val.name;
  } else if (opts.prefixed_enums) {
      return enum_def.name + "_" + enum_val.name;
  } else {
      return enum_val.name;
  }
}

std::string EnumSignature(EnumDef &enum_def) {
  return "inline bool Verify" + enum_def.name +
         "(flatbuffers::Verifier &verifier, " +
         "const void *union_obj, " + enum_def.name + " type)";
}

// Generate an enum declaration and an enum string lookup table.
static void GenEnum(const Parser &parser, EnumDef &enum_def,
                    std::string *code_ptr) {
  std::string &code = *code_ptr;
  GenComment(enum_def.doc_comment, code_ptr, nullptr);
  code += GenEnumDecl(enum_def, parser.opts);
  if (parser.opts.scoped_enums)
    code += " : " + GenTypeBasic(enum_def.underlying_type, false);
  code += " {\n";
  EnumVal *minv = nullptr, *maxv = nullptr;
  for (auto it = enum_def.vals.vec.begin();
       it != enum_def.vals.vec.end();
       ++it) {
    auto &ev = **it;
    GenComment(ev.doc_comment, code_ptr, nullptr, "  ");
    code += "  " + GenEnumVal(enum_def, ev.name, parser.opts) + " = ";
    code += NumToString(ev.value) + ",\n";
    minv = !minv || minv->value > ev.value ? &ev : minv;
    maxv = !maxv || maxv->value < ev.value ? &ev : maxv;
  }
  assert(minv && maxv);
  code += "  " + GenEnumVal(enum_def, "MIN", parser.opts) + " = ";
  code += GenEnumVal(enum_def, minv->name, parser.opts) + ",\n";
  code += "  " + GenEnumVal(enum_def, "MAX", parser.opts) + " = ";
  code += GenEnumVal(enum_def, maxv->name, parser.opts) + "\n";
  code += "};\n";
  if (parser.opts.scoped_enums && enum_def.attributes.Lookup("bit_flags"))
    code += "DEFINE_BITMASK_OPERATORS(" + enum_def.name + ", " +  GenTypeBasic(enum_def.underlying_type, false) + ")\n";
  code += "\n";

  // Generate a generate string table for enum values.
  // Problem is, if values are very sparse that could generate really big
  // tables. Ideally in that case we generate a map lookup instead, but for
  // the moment we simply don't output a table at all.
  auto range = enum_def.vals.vec.back()->value -
               enum_def.vals.vec.front()->value + 1;<commit_msg>MIN & MAX are useless for bit_flags<commit_after>/*
 * Copyright 2014 Google Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

// independent from idl_parser, since this code is not needed for most clients

#include "flatbuffers/flatbuffers.h"
#include "flatbuffers/idl.h"
#include "flatbuffers/util.h"
#include "flatbuffers/code_generators.h"

namespace flatbuffers {
namespace cpp {

// This tracks the current namespace so we can insert namespace declarations.
// TODO(wvo): this needs to be moved into a code generator context object.
static const Namespace *code_generator_cur_name_space = nullptr;

// Ensure that a type is prefixed with its namespace whenever it is used
// outside of its namespace.
static std::string WrapInNameSpace(const Namespace *ns,
                                   const std::string &name) {
  if (code_generator_cur_name_space != ns) {
    std::string qualified_name;
    for (auto it = ns->components.begin();
             it != ns->components.end(); ++it) {
      qualified_name += *it + "::";
    }
    return qualified_name + name;
  } else {
    return name;
  }
}

static std::string WrapInNameSpace(const Definition &def) {
  return WrapInNameSpace(def.defined_namespace, def.name);
}

// Translates a qualified name in flatbuffer text format to the same name in
// the equivalent C++ namespace.
static std::string TranslateNameSpace(const std::string &qualified_name) {
  std::string cpp_qualified_name = qualified_name;
  size_t start_pos = 0;
  while((start_pos = cpp_qualified_name.find(".", start_pos)) !=
         std::string::npos) {
    cpp_qualified_name.replace(start_pos, 1, "::");
  }
  return cpp_qualified_name;
}


// Return a C++ type from the table in idl.h
static std::string GenTypeBasic(const Type &type, bool user_facing_type) {
  static const char *ctypename[] = {
    #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE, NTYPE, PTYPE) \
      #CTYPE,
      FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
    #undef FLATBUFFERS_TD
  };
  if (user_facing_type) {
    if (type.enum_def) return WrapInNameSpace(*type.enum_def);
    if (type.base_type == BASE_TYPE_BOOL) return "bool";
  }
  return ctypename[type.base_type];
}

static std::string GenTypeWire(const Parser &parser, const Type &type,
                               const char *postfix, bool user_facing_type);

// Return a C++ pointer type, specialized to the actual struct/table types,
// and vector element types.
static std::string GenTypePointer(const Parser &parser, const Type &type) {
  switch (type.base_type) {
    case BASE_TYPE_STRING:
      return "flatbuffers::String";
    case BASE_TYPE_VECTOR:
      return "flatbuffers::Vector<" +
             GenTypeWire(parser, type.VectorType(), "", false) + ">";
    case BASE_TYPE_STRUCT: {
      return WrapInNameSpace(*type.struct_def);
    }
    case BASE_TYPE_UNION:
      // fall through
    default:
      return "void";
  }
}

// Return a C++ type for any type (scalar/pointer) specifically for
// building a flatbuffer.
static std::string GenTypeWire(const Parser &parser, const Type &type,
                               const char *postfix, bool user_facing_type) {
  return IsScalar(type.base_type)
    ? GenTypeBasic(type, user_facing_type) + postfix
    : IsStruct(type)
      ? "const " + GenTypePointer(parser, type) + " *"
      : "flatbuffers::Offset<" + GenTypePointer(parser, type) + ">" + postfix;
}

// Return a C++ type for any type (scalar/pointer) that reflects its
// serialized size.
static std::string GenTypeSize(const Parser &parser, const Type &type) {
  return IsScalar(type.base_type)
    ? GenTypeBasic(type, false)
    : IsStruct(type)
      ? GenTypePointer(parser, type)
      : "flatbuffers::uoffset_t";
}

// Return a C++ type for any type (scalar/pointer) specifically for
// using a flatbuffer.
static std::string GenTypeGet(const Parser &parser, const Type &type,
                              const char *afterbasic, const char *beforeptr,
                              const char *afterptr, bool user_facing_type) {
  return IsScalar(type.base_type)
    ? GenTypeBasic(type, user_facing_type) + afterbasic
    : beforeptr + GenTypePointer(parser, type) + afterptr;
}

static std::string GenEnumDecl(const EnumDef &enum_def,
                               const IDLOptions &opts) {
  return (opts.scoped_enums ? "enum class " : "enum ") + enum_def.name;
}

static std::string GenEnumVal(const EnumDef &enum_def,
                              const std::string &enum_val,
                              const IDLOptions &opts) {
  return opts.prefixed_enums ? enum_def.name + "_" + enum_val : enum_val;
}

static std::string GetEnumVal(const EnumDef &enum_def, const EnumVal &enum_val,
                              const IDLOptions &opts) {
  if (opts.scoped_enums) {
      return enum_def.name + "::" + enum_val.name;
  } else if (opts.prefixed_enums) {
      return enum_def.name + "_" + enum_val.name;
  } else {
      return enum_val.name;
  }
}

std::string EnumSignature(EnumDef &enum_def) {
  return "inline bool Verify" + enum_def.name +
         "(flatbuffers::Verifier &verifier, " +
         "const void *union_obj, " + enum_def.name + " type)";
}

// Generate an enum declaration and an enum string lookup table.
static void GenEnum(const Parser &parser, EnumDef &enum_def,
                    std::string *code_ptr) {
  std::string &code = *code_ptr;
  GenComment(enum_def.doc_comment, code_ptr, nullptr);
  code += GenEnumDecl(enum_def, parser.opts);
  if (parser.opts.scoped_enums)
    code += " : " + GenTypeBasic(enum_def.underlying_type, false);
  code += " {\n";
  int64_t anyv = 0;
  EnumVal *minv = nullptr, *maxv = nullptr;
  for (auto it = enum_def.vals.vec.begin();
       it != enum_def.vals.vec.end();
       ++it) {
    auto &ev = **it;
    GenComment(ev.doc_comment, code_ptr, nullptr, "  ");
    code += "  " + GenEnumVal(enum_def, ev.name, parser.opts) + " = ";
    code += NumToString(ev.value) + ",\n";
    minv = !minv || minv->value > ev.value ? &ev : minv;
    maxv = !maxv || maxv->value < ev.value ? &ev : maxv;
    anyv |= ev.value;
  }
  assert(minv && maxv);
  if (enum_def.attributes.Lookup("bit_flags")) {
    if (minv->value != 0) // If the user didn't defined NONE value
      code += "  " + GenEnumVal(enum_def, "NONE", parser.opts) + " = 0,\n";
    if (maxv->value != anyv) // If the user didn't defined ANY value
      code += "  " + GenEnumVal(enum_def, "ANY", parser.opts) + " = " + NumToString(anyv) + "\n";
  } else { // MIN & MAX are useless for bit_flags
    code += "  " + GenEnumVal(enum_def, "MIN", parser.opts) + " = ";
    code += GenEnumVal(enum_def, minv->name, parser.opts) + ",\n";
    code += "  " + GenEnumVal(enum_def, "MAX", parser.opts) + " = ";
    code += GenEnumVal(enum_def, maxv->name, parser.opts) + "\n";
  }
  code += "};\n";
  if (parser.opts.scoped_enums && enum_def.attributes.Lookup("bit_flags"))
    code += "DEFINE_BITMASK_OPERATORS(" + enum_def.name + ", " +  GenTypeBasic(enum_def.underlying_type, false) + ")\n";
  code += "\n";

  // Generate a generate string table for enum values.
  // Problem is, if values are very sparse that could generate really big
  // tables. Ideally in that case we generate a map lookup instead, but for
  // the moment we simply don't output a table at all.
  auto range = enum_def.vals.vec.back()->value -
               enum_def.vals.vec.front()->value + 1;
{'fraction_non_alphanumeric': 0.12177361679994798, 'fraction_numerical': 0.0017554125219426564, 'mean_word_length': 3.161796536796537, 'pattern_counts': {'":': 6, '<': 9, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5481965', 'n_tokens_mistral': 4916, 'n_tokens_neox': 4848, 'n_words': 1377}