blob: d64ab03c143644dad9d31179afef968a98087987 [file] [log] [blame]
Austin Schuhe89fa2d2019-08-14 20:24:23 -07001/*
2 * Copyright 2014 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#ifndef FLATBUFFERS_CODE_GENERATORS_H_
18#define FLATBUFFERS_CODE_GENERATORS_H_
19
20#include <map>
21#include <sstream>
Austin Schuh272c6132020-11-14 16:37:52 -080022
Austin Schuhe89fa2d2019-08-14 20:24:23 -070023#include "flatbuffers/idl.h"
24
25namespace flatbuffers {
26
27// Utility class to assist in generating code through use of text templates.
28//
29// Example code:
30// CodeWriter code("\t");
31// code.SetValue("NAME", "Foo");
32// code += "void {{NAME}}() { printf("%s", "{{NAME}}"); }";
33// code.SetValue("NAME", "Bar");
34// code += "void {{NAME}}() { printf("%s", "{{NAME}}"); }";
35// std::cout << code.ToString() << std::endl;
36//
37// Output:
38// void Foo() { printf("%s", "Foo"); }
39// void Bar() { printf("%s", "Bar"); }
40class CodeWriter {
41 public:
42 CodeWriter(std::string pad = std::string())
43 : pad_(pad), cur_ident_lvl_(0), ignore_ident_(false) {}
44
45 // Clears the current "written" code.
46 void Clear() {
47 stream_.str("");
48 stream_.clear();
49 }
50
51 // Associates a key with a value. All subsequent calls to operator+=, where
52 // the specified key is contained in {{ and }} delimiters will be replaced by
53 // the given value.
54 void SetValue(const std::string &key, const std::string &value) {
55 value_map_[key] = value;
56 }
57
58 std::string GetValue(const std::string &key) const {
59 const auto it = value_map_.find(key);
60 return it == value_map_.end() ? "" : it->second;
61 }
62
63 // Appends the given text to the generated code as well as a newline
Austin Schuh272c6132020-11-14 16:37:52 -080064 // character. Any text within {{ and }} delimiters is replaced by values
Austin Schuhe89fa2d2019-08-14 20:24:23 -070065 // previously stored in the CodeWriter by calling SetValue above. The newline
66 // will be suppressed if the text ends with the \\ character.
67 void operator+=(std::string text);
68
69 // Returns the current contents of the CodeWriter as a std::string.
70 std::string ToString() const { return stream_.str(); }
71
72 // Increase ident level for writing code
73 void IncrementIdentLevel() { cur_ident_lvl_++; }
74 // Decrease ident level for writing code
75 void DecrementIdentLevel() {
76 if (cur_ident_lvl_) cur_ident_lvl_--;
77 }
78
Austin Schuh272c6132020-11-14 16:37:52 -080079 void SetPadding(const std::string &padding) { pad_ = padding; }
80
Austin Schuhe89fa2d2019-08-14 20:24:23 -070081 private:
82 std::map<std::string, std::string> value_map_;
83 std::stringstream stream_;
84 std::string pad_;
85 int cur_ident_lvl_;
86 bool ignore_ident_;
87
88 // Add ident padding (tab or space) based on ident level
89 void AppendIdent(std::stringstream &stream);
90};
91
92class BaseGenerator {
93 public:
94 virtual bool generate() = 0;
95
96 static std::string NamespaceDir(const Parser &parser, const std::string &path,
97 const Namespace &ns);
98
Austin Schuh272c6132020-11-14 16:37:52 -080099 std::string GeneratedFileName(const std::string &path,
100 const std::string &file_name,
101 const IDLOptions &options) const;
102
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700103 protected:
104 BaseGenerator(const Parser &parser, const std::string &path,
Austin Schuh272c6132020-11-14 16:37:52 -0800105 const std::string &file_name, std::string qualifying_start,
106 std::string qualifying_separator, std::string default_extension)
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700107 : parser_(parser),
108 path_(path),
109 file_name_(file_name),
110 qualifying_start_(qualifying_start),
Austin Schuh272c6132020-11-14 16:37:52 -0800111 qualifying_separator_(qualifying_separator),
112 default_extension_(default_extension) {}
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700113 virtual ~BaseGenerator() {}
114
115 // No copy/assign.
116 BaseGenerator &operator=(const BaseGenerator &);
117 BaseGenerator(const BaseGenerator &);
118
119 std::string NamespaceDir(const Namespace &ns) const;
120
121 static const char *FlatBuffersGeneratedWarning();
122
123 static std::string FullNamespace(const char *separator, const Namespace &ns);
124
125 static std::string LastNamespacePart(const Namespace &ns);
126
127 // tracks the current namespace for early exit in WrapInNameSpace
128 // c++, java and csharp returns a different namespace from
129 // the following default (no early exit, always fully qualify),
130 // which works for js and php
131 virtual const Namespace *CurrentNameSpace() const { return nullptr; }
132
133 // Ensure that a type is prefixed with its namespace even within
134 // its own namespace to avoid conflict between generated method
135 // names and similarly named classes or structs
136 std::string WrapInNameSpace(const Namespace *ns,
137 const std::string &name) const;
138
139 std::string WrapInNameSpace(const Definition &def) const;
140
141 std::string GetNameSpace(const Definition &def) const;
142
143 const Parser &parser_;
144 const std::string &path_;
145 const std::string &file_name_;
146 const std::string qualifying_start_;
147 const std::string qualifying_separator_;
Austin Schuh272c6132020-11-14 16:37:52 -0800148 const std::string default_extension_;
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700149};
150
151struct CommentConfig {
152 const char *first_line;
153 const char *content_line_prefix;
154 const char *last_line;
155};
156
157extern void GenComment(const std::vector<std::string> &dc,
158 std::string *code_ptr, const CommentConfig *config,
159 const char *prefix = "");
160
161class FloatConstantGenerator {
162 public:
163 virtual ~FloatConstantGenerator() {}
164 std::string GenFloatConstant(const FieldDef &field) const;
165
166 private:
167 virtual std::string Value(double v, const std::string &src) const = 0;
168 virtual std::string Inf(double v) const = 0;
169 virtual std::string NaN(double v) const = 0;
170
171 virtual std::string Value(float v, const std::string &src) const = 0;
172 virtual std::string Inf(float v) const = 0;
173 virtual std::string NaN(float v) const = 0;
174
175 template<typename T>
176 std::string GenFloatConstantImpl(const FieldDef &field) const;
177};
178
179class SimpleFloatConstantGenerator : public FloatConstantGenerator {
180 public:
181 SimpleFloatConstantGenerator(const char *nan_number,
182 const char *pos_inf_number,
183 const char *neg_inf_number);
184
185 private:
186 std::string Value(double v,
187 const std::string &src) const FLATBUFFERS_OVERRIDE;
188 std::string Inf(double v) const FLATBUFFERS_OVERRIDE;
189 std::string NaN(double v) const FLATBUFFERS_OVERRIDE;
190
191 std::string Value(float v, const std::string &src) const FLATBUFFERS_OVERRIDE;
192 std::string Inf(float v) const FLATBUFFERS_OVERRIDE;
193 std::string NaN(float v) const FLATBUFFERS_OVERRIDE;
194
195 const std::string nan_number_;
196 const std::string pos_inf_number_;
197 const std::string neg_inf_number_;
198};
199
200// C++, C#, Java like generator.
201class TypedFloatConstantGenerator : public FloatConstantGenerator {
202 public:
203 TypedFloatConstantGenerator(const char *double_prefix,
204 const char *single_prefix, const char *nan_number,
205 const char *pos_inf_number,
206 const char *neg_inf_number = "");
207
208 private:
209 std::string Value(double v,
210 const std::string &src) const FLATBUFFERS_OVERRIDE;
211 std::string Inf(double v) const FLATBUFFERS_OVERRIDE;
212
213 std::string NaN(double v) const FLATBUFFERS_OVERRIDE;
214
215 std::string Value(float v, const std::string &src) const FLATBUFFERS_OVERRIDE;
216 std::string Inf(float v) const FLATBUFFERS_OVERRIDE;
217 std::string NaN(float v) const FLATBUFFERS_OVERRIDE;
218
219 std::string MakeNaN(const std::string &prefix) const;
220 std::string MakeInf(bool neg, const std::string &prefix) const;
221
222 const std::string double_prefix_;
223 const std::string single_prefix_;
224 const std::string nan_number_;
225 const std::string pos_inf_number_;
226 const std::string neg_inf_number_;
227};
228
229} // namespace flatbuffers
230
231#endif // FLATBUFFERS_CODE_GENERATORS_H_