Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2016 Google Inc. All rights reserved. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "flatbuffers/code_generators.h" |
| 18 | #include <assert.h> |
| 19 | #include "flatbuffers/base.h" |
| 20 | #include "flatbuffers/util.h" |
| 21 | |
| 22 | #include <cmath> |
| 23 | |
| 24 | #if defined(_MSC_VER) |
| 25 | # pragma warning(push) |
| 26 | # pragma warning(disable : 4127) // C4127: conditional expression is constant |
| 27 | #endif |
| 28 | |
| 29 | namespace flatbuffers { |
| 30 | |
| 31 | void CodeWriter::operator+=(std::string text) { |
| 32 | if (!ignore_ident_ && !text.empty()) AppendIdent(stream_); |
| 33 | |
| 34 | while (true) { |
| 35 | auto begin = text.find("{{"); |
| 36 | if (begin == std::string::npos) { break; } |
| 37 | |
| 38 | auto end = text.find("}}"); |
| 39 | if (end == std::string::npos || end < begin) { break; } |
| 40 | |
| 41 | // Write all the text before the first {{ into the stream. |
| 42 | stream_.write(text.c_str(), begin); |
| 43 | |
| 44 | // The key is between the {{ and }}. |
| 45 | const std::string key = text.substr(begin + 2, end - begin - 2); |
| 46 | |
| 47 | // Find the value associated with the key. If it exists, write the |
| 48 | // value into the stream, otherwise write the key itself into the stream. |
| 49 | auto iter = value_map_.find(key); |
| 50 | if (iter != value_map_.end()) { |
| 51 | const std::string &value = iter->second; |
| 52 | stream_ << value; |
| 53 | } else { |
| 54 | FLATBUFFERS_ASSERT(false && "could not find key"); |
| 55 | stream_ << key; |
| 56 | } |
| 57 | |
| 58 | // Update the text to everything after the }}. |
| 59 | text = text.substr(end + 2); |
| 60 | } |
| 61 | if (!text.empty() && string_back(text) == '\\') { |
| 62 | text.pop_back(); |
| 63 | ignore_ident_ = true; |
| 64 | stream_ << text; |
| 65 | } else { |
| 66 | ignore_ident_ = false; |
| 67 | stream_ << text << std::endl; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | void CodeWriter::AppendIdent(std::stringstream &stream) { |
| 72 | int lvl = cur_ident_lvl_; |
| 73 | while (lvl--) { |
| 74 | stream.write(pad_.c_str(), static_cast<std::streamsize>(pad_.size())); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | const char *BaseGenerator::FlatBuffersGeneratedWarning() { |
| 79 | return "automatically generated by the FlatBuffers compiler," |
| 80 | " do not modify"; |
| 81 | } |
| 82 | |
| 83 | std::string BaseGenerator::NamespaceDir(const Parser &parser, |
| 84 | const std::string &path, |
| 85 | const Namespace &ns) { |
| 86 | EnsureDirExists(path); |
| 87 | if (parser.opts.one_file) return path; |
| 88 | std::string namespace_dir = path; // Either empty or ends in separator. |
| 89 | auto &namespaces = ns.components; |
| 90 | for (auto it = namespaces.begin(); it != namespaces.end(); ++it) { |
| 91 | namespace_dir += *it + kPathSeparator; |
| 92 | EnsureDirExists(namespace_dir); |
| 93 | } |
| 94 | return namespace_dir; |
| 95 | } |
| 96 | |
| 97 | std::string BaseGenerator::NamespaceDir(const Namespace &ns) const { |
| 98 | return BaseGenerator::NamespaceDir(parser_, path_, ns); |
| 99 | } |
| 100 | |
| 101 | std::string BaseGenerator::FullNamespace(const char *separator, |
| 102 | const Namespace &ns) { |
| 103 | std::string namespace_name; |
| 104 | auto &namespaces = ns.components; |
| 105 | for (auto it = namespaces.begin(); it != namespaces.end(); ++it) { |
| 106 | if (namespace_name.length()) namespace_name += separator; |
| 107 | namespace_name += *it; |
| 108 | } |
| 109 | return namespace_name; |
| 110 | } |
| 111 | |
| 112 | std::string BaseGenerator::LastNamespacePart(const Namespace &ns) { |
| 113 | if (!ns.components.empty()) |
| 114 | return ns.components.back(); |
| 115 | else |
| 116 | return std::string(""); |
| 117 | } |
| 118 | |
| 119 | // Ensure that a type is prefixed with its namespace. |
| 120 | std::string BaseGenerator::WrapInNameSpace(const Namespace *ns, |
| 121 | const std::string &name) const { |
| 122 | std::string qualified_name = qualifying_start_; |
| 123 | for (auto it = ns->components.begin(); it != ns->components.end(); ++it) |
| 124 | qualified_name += *it + qualifying_separator_; |
| 125 | return qualified_name + name; |
| 126 | } |
| 127 | |
| 128 | std::string BaseGenerator::WrapInNameSpace(const Definition &def) const { |
| 129 | return WrapInNameSpace(def.defined_namespace, def.name); |
| 130 | } |
| 131 | |
| 132 | std::string BaseGenerator::GetNameSpace(const Definition &def) const { |
| 133 | const Namespace *ns = def.defined_namespace; |
| 134 | if (CurrentNameSpace() == ns) return ""; |
| 135 | std::string qualified_name = qualifying_start_; |
| 136 | for (auto it = ns->components.begin(); it != ns->components.end(); ++it) { |
| 137 | qualified_name += *it; |
| 138 | if ((it + 1) != ns->components.end()) { |
| 139 | qualified_name += qualifying_separator_; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | return qualified_name; |
| 144 | } |
| 145 | |
| 146 | // Generate a documentation comment, if available. |
| 147 | void GenComment(const std::vector<std::string> &dc, std::string *code_ptr, |
| 148 | const CommentConfig *config, const char *prefix) { |
| 149 | if (dc.begin() == dc.end()) { |
| 150 | // Don't output empty comment blocks with 0 lines of comment content. |
| 151 | return; |
| 152 | } |
| 153 | |
| 154 | std::string &code = *code_ptr; |
| 155 | if (config != nullptr && config->first_line != nullptr) { |
| 156 | code += std::string(prefix) + std::string(config->first_line) + "\n"; |
| 157 | } |
| 158 | std::string line_prefix = |
| 159 | std::string(prefix) + |
| 160 | ((config != nullptr && config->content_line_prefix != nullptr) |
| 161 | ? config->content_line_prefix |
| 162 | : "///"); |
| 163 | for (auto it = dc.begin(); it != dc.end(); ++it) { |
| 164 | code += line_prefix + *it + "\n"; |
| 165 | } |
| 166 | if (config != nullptr && config->last_line != nullptr) { |
| 167 | code += std::string(prefix) + std::string(config->last_line) + "\n"; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | template<typename T> |
| 172 | std::string FloatConstantGenerator::GenFloatConstantImpl( |
| 173 | const FieldDef &field) const { |
| 174 | const auto &constant = field.value.constant; |
| 175 | T v; |
| 176 | auto done = StringToNumber(constant.c_str(), &v); |
| 177 | FLATBUFFERS_ASSERT(done); |
| 178 | if (done) { |
| 179 | #if (!defined(_MSC_VER) || (_MSC_VER >= 1800)) |
| 180 | if (std::isnan(v)) return NaN(v); |
| 181 | if (std::isinf(v)) return Inf(v); |
| 182 | #endif |
| 183 | return Value(v, constant); |
| 184 | } |
| 185 | return "#"; // compile time error |
| 186 | } |
| 187 | |
| 188 | std::string FloatConstantGenerator::GenFloatConstant( |
| 189 | const FieldDef &field) const { |
| 190 | switch (field.value.type.base_type) { |
| 191 | case BASE_TYPE_FLOAT: return GenFloatConstantImpl<float>(field); |
| 192 | case BASE_TYPE_DOUBLE: return GenFloatConstantImpl<double>(field); |
| 193 | default: { |
| 194 | FLATBUFFERS_ASSERT(false); |
| 195 | return "INVALID_BASE_TYPE"; |
| 196 | } |
| 197 | }; |
| 198 | } |
| 199 | |
| 200 | TypedFloatConstantGenerator::TypedFloatConstantGenerator( |
| 201 | const char *double_prefix, const char *single_prefix, |
| 202 | const char *nan_number, const char *pos_inf_number, |
| 203 | const char *neg_inf_number) |
| 204 | : double_prefix_(double_prefix), |
| 205 | single_prefix_(single_prefix), |
| 206 | nan_number_(nan_number), |
| 207 | pos_inf_number_(pos_inf_number), |
| 208 | neg_inf_number_(neg_inf_number) {} |
| 209 | |
| 210 | std::string TypedFloatConstantGenerator::MakeNaN( |
| 211 | const std::string &prefix) const { |
| 212 | return prefix + nan_number_; |
| 213 | } |
| 214 | std::string TypedFloatConstantGenerator::MakeInf( |
| 215 | bool neg, const std::string &prefix) const { |
| 216 | if (neg) |
| 217 | return !neg_inf_number_.empty() ? (prefix + neg_inf_number_) |
| 218 | : ("-" + prefix + pos_inf_number_); |
| 219 | else |
| 220 | return prefix + pos_inf_number_; |
| 221 | } |
| 222 | |
| 223 | std::string TypedFloatConstantGenerator::Value(double v, |
| 224 | const std::string &src) const { |
| 225 | (void)v; |
| 226 | return src; |
| 227 | } |
| 228 | |
| 229 | std::string TypedFloatConstantGenerator::Inf(double v) const { |
| 230 | return MakeInf(v < 0, double_prefix_); |
| 231 | } |
| 232 | |
| 233 | std::string TypedFloatConstantGenerator::NaN(double v) const { |
| 234 | (void)v; |
| 235 | return MakeNaN(double_prefix_); |
| 236 | } |
| 237 | |
| 238 | std::string TypedFloatConstantGenerator::Value(float v, |
| 239 | const std::string &src) const { |
| 240 | (void)v; |
| 241 | return src + "f"; |
| 242 | } |
| 243 | |
| 244 | std::string TypedFloatConstantGenerator::Inf(float v) const { |
| 245 | return MakeInf(v < 0, single_prefix_); |
| 246 | } |
| 247 | |
| 248 | std::string TypedFloatConstantGenerator::NaN(float v) const { |
| 249 | (void)v; |
| 250 | return MakeNaN(single_prefix_); |
| 251 | } |
| 252 | |
| 253 | SimpleFloatConstantGenerator::SimpleFloatConstantGenerator( |
| 254 | const char *nan_number, const char *pos_inf_number, |
| 255 | const char *neg_inf_number) |
| 256 | : nan_number_(nan_number), |
| 257 | pos_inf_number_(pos_inf_number), |
| 258 | neg_inf_number_(neg_inf_number) {} |
| 259 | |
| 260 | std::string SimpleFloatConstantGenerator::Value(double v, |
| 261 | const std::string &src) const { |
| 262 | (void)v; |
| 263 | return src; |
| 264 | } |
| 265 | |
| 266 | std::string SimpleFloatConstantGenerator::Inf(double v) const { |
| 267 | return (v < 0) ? neg_inf_number_ : pos_inf_number_; |
| 268 | } |
| 269 | |
| 270 | std::string SimpleFloatConstantGenerator::NaN(double v) const { |
| 271 | (void)v; |
| 272 | return nan_number_; |
| 273 | } |
| 274 | |
| 275 | std::string SimpleFloatConstantGenerator::Value(float v, |
| 276 | const std::string &src) const { |
| 277 | return this->Value(static_cast<double>(v), src); |
| 278 | } |
| 279 | |
| 280 | std::string SimpleFloatConstantGenerator::Inf(float v) const { |
| 281 | return this->Inf(static_cast<double>(v)); |
| 282 | } |
| 283 | |
| 284 | std::string SimpleFloatConstantGenerator::NaN(float v) const { |
| 285 | return this->NaN(static_cast<double>(v)); |
| 286 | } |
| 287 | |
| 288 | } // namespace flatbuffers |
| 289 | |
| 290 | #if defined(_MSC_VER) |
| 291 | # pragma warning(pop) |
| 292 | #endif |